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

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

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

Required_with validation for array

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

Laravel Custom Validation Rules should be lowercase?

I have model, and it has several field names, and 'lastName' is among them.
In my FormRequest file, I have rules and messages for this field:
$rules = ['lastName.*' => 'lastName_fail: index'];
$messages = ['lastName.*lastName_fail' => This lastName has different value in DB!'];
When I am submitting a form, filling the 'lastName' field with intentionally 'wrong' value, it doesn't pass validation, and returns error message:
validation.last_name_fail
(which is not what's in $messages).
But when I change $rules and $messages to:
$rules = ['lastName.*' => 'lastname_fail: index'];
$messages = ['lastName.*lastname_fail' => This lastName has different value in DB!'];
(so the actual "rule" is now lowercase "lastname_fail"), it outputs what i want:
This lastName has different value in DB!
from this I may conclude that Laravel's validation rule name may be
only lowercase.
Is it declared anywhere in documentation?
If so, maybe it helps someone.
It is not mentioned in the documentation. However, there is a naming pattern for both validation rule method name and rule name.
Rule Method Name:
It must have validate prefix and the rest of it must be in Camel Case.
Rule Name:
It will be in lowercase without the validate prefix and each word will be separated by an underscore.
So if you want to add alpha_dash_spaces validation rule then the corresponding method will be named validateAlphaDashSpaces().
Simply parse $request[data] before Validator
use Illuminate\Support\Str;
$request['name_it'] = Str::lower($request['name_it']);
$request['name_en'] = Str::lower($request['name_en']);
$validator = Validator::make($request->all(), [
'name_it' => ['required', 'string', 'max:255', 'unique:categories'],
'name_en' => ['required', 'string', 'max:255', 'unique:categories'],
]);
if ($validator->fails()) {
return redirect()
->back()->withErrors($validator)
->withInput();
}

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