Laravel array object rule - php

I send an array to a REST API. How can I add a rule for the array?
Also I want to add field_name_id, field_input_type and field_caption as required fields.
I don't know how can I access the array in Laravel rules. Can someone help me?
$rules = [
'name' => 'required',
'forms' => 'array'
]

Laravel uses dot notation to validate arrays and it's nested fields.
$rules = [
'forms.field_name_id' => 'required',
'forms.field_input_type'=> 'required',
'forms.field_caption' => 'required',
]
You can also validate each value within the array. For example, If you want the caption to be unique:
$rules = [
'forms.*.field_caption' => 'unique:captions,caption',
]
Here are the docs for more information on how to use them

Related

Laravel validate array and exclude on specific array value

I have an array of values that I send together with other fields in a form to laravel.
The array contains a role_id field and a status field, the status can be I (Insert) U (update) D (Delete). When I validate the values in the array I want it to skip the ones where the status equals D. Otherwise I want it to validate.
private function checkValuesUpdate($userid = null)
{
return Request::validate(
[
'displayname' => 'required',
'username' => 'required',
'email' => ['nullable', 'unique:contacts,email' . (is_null($userid ) ? '' : (',' . $userid ))],
'roles.*.role_id' => ['exclude_if:roles.*.status,D', 'required']
]
);
}
I can't seem to get it to work. I've been searching all over the place for functional code as well as the Laravel documentation. But no luck so far. Has anybody ever done this?
Your problem comes from a misunderstanding of the exclude_if rule. It doesn't exclude the value from validation, it only excludes the value from the returned data. So it would not be included if you ran request()->validated() to get the validated input values.
According to the documentation, you can use validation rules with array/star notation so using the required_unless rule might be a better approach. (Note there's also more concise code to replace the old unique rule, and I've added a rule to check contents of the role status.)
$rules = [
'displayname' => 'required',
'username' => 'required',
'email' => [
'nullable',
Rule::unique("contacts")->ignore($userid ?? 0)
],
'roles' => 'array',
'roles.*.status' => 'in:I,U,D',
'roles.*.role_id' => ['required_unless:roles.*.status,D']
];

Using excludes_unless validation rule with arrays in Laravel

Laravel's exclude_unless,field,value rule doesn't seem to work in the instance where field is an array of values and value is contained within the field array.
Given the following:
$validator = Validator::make($request->all(), [
'some_array' => 'array',
'some_field' => 'exclude_unless:some_array,foo|alpha_num',
]);
Where some_array is equal to ['foo'], the validator will fail to exclude some_field. This seems to be because the comparison that lies beneath excludes_unless is in_array(['foo'], ['foo']) which returns false.
Is it possible to achieve the same logic of exclude_unless — excluding a field from validation if another field equals a certain value, but where the field being compared against is an array and we're checking that the value is in that array?
As long as you're using Laravel 8.55 or above, you could use Rule::when():
use Illuminate\Validation\Rule;
$someArray = $request->input('some_array', []);
$validator = Validator::make($request->all(), [
'some_array' => 'array',
'some_field' => Rule::when(!empty(array_intersect(['foo'], $someArray)), ['exclude']),
]);

Laravel regex validation for price OR empty

I am trying to make a regex for price OR empty.
I have the price part (Dutch uses comma instead of point) which actualy works
/^\d+(,\d{1,2})?$/
The regex above validates ok on the value 21,99
Now I try to add the empty part so the field can be... just empty ^$
/(^$|^\d+(,\d{1,2})?$)/
But Laravel starts to complain as soon as I change the regex:
"Method [validate^\d+(,\d{1,2})?$)/] does not exist."
Works ok:
$rules = [
'price' => 'regex:/^\d+(,\d{1,2})?$/'
];
Laravel says no...:
$rules = [
'price' => 'regex:/(^$|^\d+(,\d{1,2})?$)/'
];
Kenken9990 answer - Laravel doesn't break anymore but an empty value is still wrong:
$rules = [
'price' => 'regex:/^(\d+(,\d{1,2})?)?$/'
];
is this work ?
$rules = [
'price' => 'nullable|regex:/^(\d+(,\d{1,2})?)?$/'
];
| is also the separator for multiple validation rules.
For example the following is valid:
$rules = [ "price" => "nullable|numeric|between:0,99" ];
To use it regex you need to switch to using an array:
$rules = [
'price' => [ 'regex:/(^$|^\d+(,\d{1,2})?$)/' ]
];
This is also pointed out in the documentation:
Note: When using the regex / not_regex patterns, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
Incidentally the original rule might also do what you want and can also be written as:
$rules [
'price' => [ 'nullable', 'numeric', 'between:0,99' ]
]

Laravel ignore id, on array ? (multiple update)

In Laravel, we can do validation on array like
$this->validate($request, [
'users.*.name' => 'bail|nullable|min:2',
'users.*.username'=>'bail|nullable|min:4|unique:t0101_user,username,' .$xx
'users.*.password' => 'bail|nullable|min:6'
], $this->messages());
In this scenario, what should i passed to the xx ?
I will need something like
users.*.id
Thank you,
(Laravel version 5.4)
Laravel Unique Validation Rule
Documentation Link
You need to manually create a validator using the unique rule
use Illuminate\Validation\Rule;
Validator::make( $data, [
'users.*.username' => [
'bail', 'nullable', 'min:4', Rule::unique( 't0101_user', 'username' )->ignore( 'users.*.id' )
]
]);

laravel validate array fields

I am trying to validate array field using laravel validate functionality as follow
$this->validate($request,['prodActualQty' => 'required|numeric','actQty[]' => 'required'
],$messages);
my input file is: <input class='form-control' type='text' name='actQty[]'>
It gives error if fields are blank but it still gives error even we fill the fields.
In Laravel 5.2 you can validate Form array elements using wildcards keyword.
So as per your situation you can either remove [] like below
$this->validate($request->all(), [
'prodActualQty' => 'required',
'actQty' => 'required'
]);
Or use wildcard operator
$this->validate($request->all(), [
'prodActualQty' => 'required',
'actQty.*' => 'required'
]);

Categories