Required_with validation for array - validation

I have a problem when I validate array with required_with validation. It is always showing even I input nothing . I got this error since two days ago. I want to check if one field insert value ,the other filed must insert value .If nothing insert , the other filed don't need to insert. Could you help me please?
This is Error I got even I input value or not . Is there any solutions for this ?
Error Message Image
Validation Request Code.
'work_procedure[]'=>'nullable|required_with:times,work_points',
'times[]'=>'nullable|required_with:work_procedure',
'work_points[]'=>'nullable|required_with:work_procedure',
This is Model.php
protected $fillable=[
'work_procedure',
'times',
'work_points',
];

https://laravel.com/docs/8.x/validation#rule-required-with-all
required_with_all:foo,bar,...
The field under validation must be present and not empty only if all of the other specified fields are present and not empty.
required_without:foo,bar,...
The field under validation must be present and not empty only when any of the other specified fields are empty or not present.
required_without_all:foo,bar,...
The field under validation must be present and not empty only when all of the other specified fields are empty or not present.
example
$request->validate([
'name' => 'required',
'surname' => 'required_with:name'
]);
$request->validate([
'name' => 'required',
'detail' => 'required|required_with:name',
]);
Validator::make($request->all(), [
'role_id' => Rule::required_with_all:foo,bar,
]);

Related

Laravel validate rule

I have two datepicker input fields: from and to. To ensure that field value to is greater than field value from, I use the after validation rule. Everything is fine up until I leave the from input value null. Because I am applying the validation rule using the input value from.
How can I combine the required and after validation rules without running into this problem?
$from = $request->input('from');
$from = Carbon::createFromFormat('Y-m-d\TH:i', $from)->format('Y-m-d H:i A');
$attributes= request()->validate([
'from' => 'required|date',
'to'=> 'nullable|date|after:'.$from,
]);
Data missing error while from input value is empty.
The laravel validation rule allows you to compare a field against another field. So you can simply add: after:from
See the documentation here.
$attributes = request()->validate([
'from' => 'required|date',
'to'=> 'nullable|date|after:from',
]);

Laravel - request validation for two nullable fields requiring at least one of them to be not-null

My model has two nullable fields, but at least one of them must be not-null.
How do I setup request validation for this ?
$request->validate([
'field_1' => ['nullable'],
'field_2' => ['nullable'],
...
]);
thanks
If you have only two filed comparisons then you can use required_without:foo,bar,...
$request->validate([
'field_1' => 'required_without:field_2',
'field_2' => 'required_without:field_1'
]);
if it has more than two fields then required_without_all:foo,bar,...
$request->validate([
'field_1' => 'required_without_all:field_2,field_3',
'field_2' => 'required_without_all:field_1,field_3',
'field_3' => 'required_without_all:field_1,field_2',
]);
As per Doc
required_without:foo,bar,...
The field under validation must be present and not empty only when any
of the other specified fields are empty or not present.
required_without_all:foo,bar,...
The field under validation must be present and not empty only when all
of the other specified fields are empty or not present.
Validation

How to validate several rules with 'required_if' on Laravel 5.5?

I am trying to validate the 'uuid' field so that it is mandatory if the 'typeUUID' field is marked 'type1'.
$validator = Validator::make($request->all(), [
'uuid' => 'required_if:typeUUID,==,type1|alpha_dash|size:36',
]);
If I select the value 'type1' it indicates that the field is mandatory, and when I set another value that is not mandatory, it validates' alpha_dash 'and' size: 36 and does not accept the field since it is sent empty.
What is the right way?
I know I could do it with a condition by checking the type at the beginning and then applying one or more rules. But I would like to know the correct way to do it.
I found the solution, is to add the rule 'nullable' after the rule 'required_if'.
$validator = Validator::make($request->all(), [
'uuid' => 'required_if:typeUUID,==,type1|nullable|alpha_dash|size:36',
]);

How to allow empty value for Laravel numeric validation

How to set not require numeric validation for Laravel5.2? I just used this Code but when i don't send value or select box haven't selected item I have error the val field most be numeric... I need if request hasn't bed input leave bed alone. leave bed validate ...
$this->validate($request, [
'provinces_id' => 'required|numeric',
'type' => 'required',
'bed' => 'numeric',
]);
If I understood you correctly, you're looking for sometimes rule:
'bed' => 'sometimes|numeric',
In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list
In Laravel 6 or 5.8, you should use nullable. But sometimes keyword doesn't work on that versions.
Use sometimes instead of required in validation rules. It checks if only there is a value. Otherwise it treats parameter as optional.
You may need nullable – sometimes and
present didn't work for me when combined with integer|min:0 on a standard text input type - the integer error was always triggered.
A Note on Optional Fields
By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. Because of this, you will often need to mark your "optional" request fields as nullable if you do not want the validator to consider null values as invalid.
Tested with Laravel 6.0-dev
Full list of available rules
In laravel 5.5 or versions after it, we begin to use nullable instead of sometimes.
according to laravel documentation 8 you must to set nullable rule
for example:
$validated = $request->validate([
'firstName' => ['required','max:255'],
'lastName' => ['required','max:255'],
'branches' => ['required'],
'services' => ['required' , 'json'],
'contract' => ['required' , 'max:255'],
'FixSalary' => ['nullable','numeric' , 'max:90000000'],
'Percent' => ['nullable','numeric' , 'max:100'],
]);
in your case :
$this->validate($request, [
'provinces_id' => 'required|numeric',
'type' => 'required',
'bed' => 'nullable|numeric',
]);

Laravel 5.2 - validate value in array

I am trying to ensure a field is valid if the value appears in a predefined array, but it's not working for me.
The validation rule I am using is:
'title' => [
'required',
'in' => ['Mr', 'Mrs', 'Miss', 'Ms'],
],
But it seems to pass validation if I enter an invalid value, such as "Dr".
Anyone know the correct way to do this?
Try with a string validation rule instead of array:
'title' => 'required|in:Mr,Mrs,Miss,Ms';

Resources