'variants' => ['nullable', 'array'],
'variants.*.name' => ['required', 'string'],
'variants.*.options' => ['required', 'array', 'min:1'],
'variants.*.options.*.code' => ['required', 'string', 'distinct'],
I'm having a validation rules above. What I'm trying to achieve is the distinct of the value only for between inner array, but somehow I'm getting an error like this with the input
input:
{
variants: [
{
name: "outer array 1",
options: [
{
code: "A"
},
{
code: "B"
}
]
},
{
name: "outer array 2",
options: [
{
code: "A"
},
]
}
]
}
result:
"error": {
"variants.0.options.0.code": [
"The variants.0.options.0.code field has a duplicate value."
],
"variants.1.options.0.code": [
"The variants.1.options.0.code field has a duplicate value."
]
}
Question: Any way to distinct only between the inner array but not every array?
using custom validation rule:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Distinct implements Rule
{
protected string $message;
protected string $strict = '';
public function __construct(bool $strict)
{
$this->strict = $strict ? ':strict' : '';
}
/**
* #param string $attribute
* #param array $value
* #return bool
*/
public function passes($attribute, $value)
{
try {
$validation = \Validator::make(['array' => $value], [
'array.*' => ["distinct{$this->strict}"]
]);
$this->message = 'The field has a duplicate value.';
return !$validation->fails();
} catch (\Exception $exception) {
$this->message = "array error";
return false;
}
}
public function message()
{
return $this->message;
}
}
Not sure if you've already worked it out, but I encountered the same issue and here is my workaround:
$rule = [
...,
'variants.*.options.0.code' => ['required', 'string', 'distinct'],
'variants.*.options.1.code' => ['required', 'string', 'distinct'],
]
If you want to apply 'distinct' rule on each individual item's array elements, you need to specify index specifically. If validating like 'variants.*.options.*.code' => ['required', 'string', 'distinct'], it will take into account array elements of other items too.
At the moment, I am not figuring out the reason why it behaves like that as when using dd($validator->getRules()), the rules processed by the validator is the same.
Any additional insight on this would be much appreciated.
Related
I am using FormRequest to validate data, which looks like this:
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules(): array
{
return [
'first_name' => ['required', 'string', 'max:255']
];
}
But if I send request like:
{
"first_name": "Random",
"last_name": "Guy"
}
Then $request->validated(), just like bellow example, is returning both, first and last names.
public function create(UserRequest $request)
{
$data = $request->validated();
return $data;
}
Any idea why is this happening? As far as I know, validated() must only return keys that exist in rules
The problem was in nesting, I was accepting data with following structure:
$rules = [
'userDetails' => ['required', 'array'],
'userDetails.firstName' => ['required', 'string'],
'userDetails.lastName' => ['required', 'string']
]
And whatever was passed in userDetails array was not being filtered, fixed by removing 'userDetails' => ['required', 'array'] from validation rules.
Looks like there's no need to use array validation
I'm trying to create two separate validation messages for the same validation attribute.
There are two rules that use "before_or_equal" - the end_time has to be "before_or_equal" start_time and also the end_time has to be after 5:00 (time). The validation works, but I can't seem to find a way to create a working custom message for the latter.
I tried to specify the rule by including it literally with the value, but it doesn't seem to work.
This is what the custom request validation looks like for the end_time at the moment.
public function rules()
{
return [
'end_time' => ['after_or_equal:start_time', 'after_or_equal:5:00'],
];
}
public function messages()
{
return [
'end_time.after_or_equal' => 'Message 1',
'end_time.after_or_equal:5:00' => 'Message 2',
];
}
You can use :date for your custom error messages.
Example:
public function rules()
{
return [
'end_time' => ['after_or_equal:start_time', 'after_or_equal:5:00'],
];
}
public function messages()
{
return [
'end_time.after_or_equal' => 'the :attribute time must be after :date',
];
}
The replaced value is actual value of first input of the validator
i don't know if i understand your question correctly, but are you looking for something like this ?
public function rules()
{
return $this->messages("end_time", [
"after_or_equal",
"after_or_equal:5:00",
]);
}
public function messages(string $Key, array $CustomAttributes)
{
$Exceptions = [
"end_time" => [
"after_or_equal" => "Message 1",
"after_or_equal:5:00" => "Message 2"
]
];
$Exception = [
$Key => []
];
foreach ($CustomAttributes as $Attribute) {
array_push($Exception[$Key], $Exceptions[$Key][$Attribute]);
}
return $Exception;
}
I have a database table structure like the following (in laravel):
user 1-1 profile
partner 1-1 profile
user 1-N department
I want to send a save request (post) and have the user validated in UserRequest and have this class call a ProfileRequest.
Is this possible to do?
Is there any way to perform validations of related models?
Class of User request example:
class UserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required|string',
'lastname' => 'required|string',
'user' => [
'required',
Rule::unique('users')->ignore($this),
],
'email' => 'required|string|email|unique:users',
'password' => 'required|string|confirmed',
'headquarter_id' => 'required'
//Validation of profile
];
}
}
Example of controller User
public function store(AdAszaUserRequest $request)
{
$input = $request->all();
$validated = $request->validated();
$input['password'] = \Hash::make($request['password']);
//
$departmentidList = array_column($input['departments'], 'id');
$AszaUser = AdAszaUser::create($input);
$models = [];
foreach ($input['departments'] as $model) {
$models[] = new AdDepartment($model);
}
///important: this line add departments without validation
$AszaUser->departments()->saveMany($models);
$AszaUser->departments()->sync($departmentidList);
return response($AszaUser, 201);
}
And Request Of deparment:
<?php
namespace App\Http\Requests\AD;
use Illuminate\Foundation\Http\FormRequest;
class AdDepartmentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required|string|unique:ad_departments',
'internal_name' => 'required|string|unique:ad_departments'
];
}
}
Example of Json send in post:
{
"id":2,
"name": "Admin2",
"email": "test#gmail.com",
"lastname": "test",
"user": "test",
"password": "test",
"password_confirmation": "test",
"headquarter_id": 1,
"lastname":"test",
"remember_token": "1",
"email_verified_at": "test",
"headquarter": {
"id": 1,
"name": "ASZA ZARAGOZA",
"description": "Sede en Zaragoza",
},
"departments": [
{
"id": 1,
"name": "Intérpretes",
"internal_name": "Interprete",
"description": "Departamento de Intérpretes",
"display_id": "01",
"pivot": {
"user_id": 1,
"department_id": 1
}
},
{
"id": 10,
"name": "Psicología"
}
]
}
Can I call the DepartmentRequest to validate the elements passed in the department array?
UPDATE: 1
I don't think it is necessary, but of course it is possible
public function store(AdAszaUserRequest $request)
{
$input = $request->all();
$validated = $request->validated();
$input['password'] = \Hash::make($request['password']);
//
$departmentidList = array_column($input['departments'], 'id');
$AszaUser = AdAszaUser::create($input);
$models = [];
foreach ($input['departments'] as $model) {
/** To check validation for single item */
$validator = Validator::make($model, (new StoreEventRequest)->rules());
if (!$validator->fails()) {
$models[] = new AdDepartment($model);
} else {
/** Something wrong */
/** $errors = $validator->errors(); */
}
}
/** To check validation for array of data
$validator = Validator::make($request->only(['departments']), collect(array_map(function ($rules, $field): array {
return ['departments.*.' . $field => $rules];
}, (new StoreEventRequest)->rules()))
->collapse()
->toArray()); */
/**
* And then do what you want to do with this object
* $errors = $validator->errors();
*
if ($validator->fails()) {
return redirect('some_url')
->withErrors($validator);
} */
$AszaUser->departments()->saveMany($models);
$AszaUser->departments()->sync($departmentidList);
return response($AszaUser, 201);
}
For more information see documentation https://laravel.com/docs/6.x/validation#manually-creating-validators
UPDATE: 2
If you need to separate your request classes, you also can do it like so
Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return array_merge([
'name' => 'required|string',
'lastname' => 'required|string',
'user' => [
'required',
Rule::unique('users')->ignore($this),
],
'email' => 'required|string|email|unique:users',
'password' => 'required|string|confirmed',
'headquarter_id' => 'required'
//Validation of profile
/** Validate of departments */
'departments' => 'nullable|array',
], collect(array_map(function ($rules, $field): array {
return ['departments.*.' . $field => $rules];
}, (new StoreEventRequest)->rules()))
->collapse()
->toArray())
->toArray();
}
}
Yes you can do it like this
public function rules() {
return [
'name' => 'required|string',
'lastname' => 'required|string',
'user' => [ 'required', Rule::unique('users')->ignore($this), ],
'email' => 'required|string|email|unique:users',
'password' => 'required|string|confirmed',
'headquarter_id' => 'required',
//Validation of profile
'profile.some_field' => 'required',
//For array of objects
'profile.*.some_field' => 'required',
];
}
I have a select list, where the first option is disabled, that's way when the user does not choose an valid option, the result of the select list will not be in the request.
In the validation, the field required, if the value of an other field is for example : 1, in case not 1, the field is not required.
The code:
'city_id' => [
'required',
'integer',
Rule::in(City::availableCities()),
],
'district_id' => new DistrictValidation(request('city_id')),
How I can do that, the district_id throw the validation every time, regardless of, it is in the request, or not.
Thanks for answers,
Update:
Maybe you see clearly, if the DistrictValidation rule is here:
class DistrictValidation implements Rule
{
protected $city;
private $messages;
/**
* Create a new rule instance.
*
* #param $cityId
*/
public function __construct($cityId)
{
$this->city = City::find($cityId);
}
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
dd('here');
if (!$this->city) {
return false;
}
if (!$this->city->hasDistrict) {
return true;
}
$validator = Validator::make([$attribute => $value], [
$attribute => [
'required',
'integer',
Rule::in(District::availableDistricts()),
]
]);
$this->messages = $validator->messages();
return $validator->passes();
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return optional($this->messages)->first('district_id');
}
}
You can use required_if condition defined in laravel validation
Here is the link to the proper documentaion Laravel Validation
Validator::make($data, [
'city_id' => [
'required',
'integer',
Rule::in(City::availableCities()),
],
'district_id'=>[
'required_with:city_id,',
]
]);
Try this:
'city_id' => [
'nullable',
'numeric',
Rule::in(City::availableCities())
],
'district_id' => new DistrictValidation(request('city_id')),
try this :
$myValidations = [
"city_id" => [
"required",
"integer"
]
]
// if city_id exists in availableCities so add some rules
if(collect(city::availableCities)->contains(request("city_id"))){
$myValidations["district_id"] = new DistrictValidation(request('city_id'))
}
// validate request fields with $myValidations variable
try using the required_if validation where the field under validation must be present and not empty if the anotherfield field is equal to any value.
required_if:field,value,...
use it like:
$request->validate([
'city_id' => 'required|integer|Rule::in(City::availableCities())',
'district_id' => 'required_if:city_id,1',
]);
try to read more laravel validation here
I have to implement the validation as mentioned in the title that either one of the two fields (email, phone) is required. I am doing this in my model:
[['email'],'either', ['other' => ['phone']]],
And this is the method:
public function either($attribute_name, $params) {
$field1 = $this->getAttributeLabel($attribute_name);
$field2 = $this->getAttributeLabel($params['other']);
if (empty($this->$attribute_name) && empty($this->$params['other'])) {
$this->addError($attribute_name, Yii::t('user', "either {$field1} or {$field2} is required."));
return false;
}
return true;
}
When I access my index page, it gives me this error:
Exception (Unknown Property) 'yii\base\UnknownPropertyException' with
message 'Setting unknown property: yii\validators\InlineValidator::0'
Any help?
If you don't care that both fields show an error when the user provides neither of both fields:
This solutions is shorter than the other answers and does not require a new validator type/class:
$rules = [
['email', 'required', 'when' => function($model) { return empty($model->phone); }],
['phone', 'required', 'when' => function($model) { return empty($model->email); }],
];
If you want to have a customized error message, just set the message option:
$rules = [
[
'email', 'required',
'message' => 'Either email or phone is required.',
'when' => function($model) { return empty($model->phone); }
],
[
'phone', 'required',
'message' => 'Either email or phone is required.',
'when' => function($model) { return empty($model->email); }
],
];
The rule should be:
['email', 'either', 'params' => ['other' => 'phone']],
And method:
public function either($attribute_name, $params)
{
$field1 = $this->getAttributeLabel($attribute_name);
$field2 = $this->getAttributeLabel($params['other']);
if (empty($this->$attribute_name) && empty($this->{$params['other']})) {
$this->addError($attribute_name, Yii::t('user', "either {$field1} or {$field2} is required."));
}
}
Improved variant
['gipsy_team_name', 'either', 'skipOnEmpty'=>false, 'params' => ['other' => 'poker_strategy_nick_name']],
['vkontakte', 'either', 'skipOnEmpty'=>false, 'params' => ['other' => ['odnoklasniki','odnoklasniki']]],
Added 'skipOnEmpty'=>false for forcing validating and 'other' can be array
/**
* validation rule
* #param string $attribute_name
* #param array $params
*/
public function either($attribute_name, $params)
{
/**
* validate actula attribute
*/
if(!empty($this->$attribute_name)){
return;
}
if(!is_array($params['other'])){
$params['other'] = [$params['other']];
}
/**
* validate other attributes
*/
foreach($params['other'] as $field){
if(!empty($this->$field)){
return;
}
}
/**
* get attributes labels
*/
$fieldsLabels = [$this->getAttributeLabel($attribute_name)];
foreach($params['other'] as $field){
$fieldsLabels[] = $this->getAttributeLabel($field);
}
$this->addError($attribute_name, \Yii::t('poker_reg', 'One of fields "{fieldList}" is required.',[
'fieldList' => implode('"", "', $fieldsLabels),
]));
}