laravel validate array fields - php

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'
]);

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']
];

Laravel Validation rules: required_without

I have two fields: Email and Telephone
i want to create a validation where one of two fields are required and if one or both fields are set, it should be the correct Format.
I tried this, but it doesnt work, i need both though
public static array $createValidationRules = [
'email' => 'required_without:telephone|email:rfc',
'telephone' => 'required_without:email|numeric|regex:/^\d{5,15}$/',
];
It is correct that both fields produce the required_without error message if both are empty. This error message clearly says that the field must be filled if the other is not. You may change the message if needed:
$messages = [
'email.required_without' => 'foo',
'telephone.required_without' => 'bar',
];
However, you must add the nullable rule, so the format rules don't apply when the field is empty:
$rules = [
'email' => ['required_without:telephone', 'nullable', 'email:rfc'],
'telephone' => ['required_without:email', 'nullable', 'numeric', 'regex:/^\d{5,15}$/'],
];
Furthermore: It is recommended writing the rules as array, especially when using regex.

Laravel old POST data empty after validation

I am printing the contents of old() in my view:
{{ print_r(old('steps'), true) }}
When I submit the form with the following validation rules, the old data prints fine:
$this->validate($request, [
'steps.*.name' => 'required',
]);
When I add more rules, the old data dissapears completely:
$this->validate($request, [
'steps.*.name' => 'required',
'steps.*.title' => 'required',
'steps.*.type' => 'required',
'steps.*.answer_options' => 'nullable|required_if:steps.*.type,Question',
'steps.*.input_type' => 'nullable|required_if:steps.*.type,Input',
]);
I've confirmed this only happens AFTER validation. How do I fix this?
Try to set SESSION_DRIVER=file to get it work
See related

Laravel/PHP: How to Validate Rendered Fields?

I have three forms, and one submit button. I only render two of the forms at any given point based on some predicate but when I submit the form, I would like the validate only the fields that exist in the currently rendered forms
Here is what the validation rules look
public function rules($request) {
return [
'name' => 'required|max:255',
'firm' => 'required|max:255',
'contactnumber' => 'required|numeric',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:'.Spark::minimumPasswordLength(),
'vat_id' => 'nullable|max:50|vat_id',
'terms' => 'required|accepted',
'accountHolderEmail' => 'required|email|max:255',
'accountHolder' => 'required|max:255',
'cardNumber' => 'required|numeric|digits:16',
'cvc' => 'required|numeric|digits:3',
'expiry_month' => array('required', 'regex:/0[1-9]|1[0-2]/'),
'expiry_year' => 'required|numeric|digits:4|date_format:Y|after:'. date('Y', strtotime('-1 years'))
];
}
I would like to validate the last six fields only when I have rendered the form that contains those fields, but I just cannot seem to get it right.
I have tried adding sometimes but that just makes the fields completely optional and that is not the desired behavior because when the form requiring those fields is rendered, we actually need the fields to be mandatory.
I have also tried using Validator::make and passing those last six fields there and that does not do it as well.
You can set flags for each form and set a value for them whether they are rendered or not.
For example
<input name="flag1" value="1"> //if form 1 is rendered
<input name="flag2" value="0"> //if form 2 is not rendered
Then you can use required_if validator like mentioned below:
'name' => 'required_if:flag1,1|max:255',
'firm' => 'required_if:flag2,1|max:255',

Laravel array object rule

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

Categories