Laravel Validation amount array of fields not work - laravel

I have created rule validation as follow :
$rules = [
'items.*.qty' => ['required', 'integer', 'min:1'],
'items.*.ordered_qty' => ['required','numeric','min:1']
]
$this->validate($request, $rules);
here i need to validate that each ordered_qty should be greater than qty I have tried the below way but it doesn't work for me
$rules = [
'items.*.qty' => ['required', 'integer', 'min:1','max:items.*.ordered_qty'],
'items.*.ordered_qty' => ['required','numeric','min:1']
]
$this->validate($request, $rules);
i tried custom validation also but i could not able to getting the value of other field
$rules = [
'items.*.qty' => ['required', 'integer', 'min:1',function ($attribute, $value, $fail) {
if ($value > ordered_qty) {
$fail(':attribute qty is invalid!');
}
}],
'items.*.ordered_qty' => ['required','numeric','min:1']
]
It validate wrong. How can i do the comparision on array of objects among their attributes

You are using the max on qty but max suspect a value while you are trying to validate it with the input of another field. I think you need to use lte, see: https://laravel.com/docs/5.8/validation#rule-lte

Related

Laravel validating that key in array is in an array of given strings

I'm building an API that takes in an array of 'additional_data' but I want some control over the fields that can be passed in.
Take the following JSON:
{
"name": "Joe Bloggs",
"additional_data": {
"type": "example",
"other_type": "example"
}
}
My current validation attempt:
return [
'name' => ['required'],
'additional_data.*' => ['sometimes', Rule::in(['type'])]
];
This always fails validation, what I'm looking for is to validate the key of the array so I can make sure the keys passed in are part of a 'whitelist'.
What you do now is you try to validate content of additional_data.type and additional_data.other_type.
You can do this by adding a custom validator. For example
Validator::extend('check_additional_data_keys', function($attribute, $value, $parameters, $validator) {
return is_array($value) && array_diff(array_keys($value), ['type', 'other_type']) === 0);
});
and use it inside your current rules
return [
'name' => ['required'],
'additional_data' => ['check_additional_data_keys'],
'additional_data.*' => ['required', 'string'],
];
Just specify your whitelist keys using the array validation rule:
return [
'name' => 'required',
'additional_data' => [
'sometimes',
'array:type',
],
];
1- In case you want applay same validation on all arrays' keys you can use the following:
return [
'name' => 'required',
'additional_data' => ['array', Rule::in(['type'])]
];
2- In case each key in the array needs different validation use the following:
return [
'name' => 'required',
'additional_data' => 'array',
'additional_data.ky1' => ['your validation here'],
'additional_data.ky2' => ['your validation here'],
];

Laravel validation depending on another model whose ID is on the same form

I have a form returning fields entity_id and price.
$validated = $request->validate([
'entity_id' => ['required', 'integer'],
'price' => ['required', 'numeric', 'min:1'],
]);
The price field should have a max rule, but that value comes from the chosen Entity that has a max_price field. That value should then be set as max. How can I do that?
You can do it with Custom validation.
One of the custom validation is to use closure
$validated = $request->validate([
'entity_id' => ['required', 'integer'],
'price' => [
'required',
'numeric',
'min:1',
function ($attribute, $value, $fail) use ($request) {
$max = Entity::where('id',$request->entity_id)->value('max_price');
//$max will be the desired value OR NULL
if (isset($max) && $max < $value) {
$fail('The '.$attribute.' is higher than the allowed max '.$max.'.');
}
}
],
]);

Laravel validation request from

I have laravel validation request form, like this:
public function rules()
{
$id = $this->input('wlId');
return [
'id' => ['sometimes ', 'integer'],
'domain_name' => ['required', 'string', 'unique:white_label,domain_name' . $id],
'disabled' => ['required', 'boolean']
];
}
I set id for ignore my entry during the unique check, but if id is't an integer but for example a string then I will get sql error from validator.
How can I stop validation if the id field fails validation?
You are going to ignore a certain value while using unique validation rule.
Consider the following snippet;
use Illuminate\Validation\Rule;
// ...
public function rules() {
$id = $this->input('wlId');
return [
'id' => ['sometimes ', 'integer'],
'domain_name' => [
'required',
'string',
Rule::unique('white_label', 'domain_name')->ignore($id)
],
'disabled' => ['required', 'boolean']
];
}
For more details, see this link; https://laravel.com/docs/8.x/validation#rule-unique
As mentioned in the documentation you can use bail rule
Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute
$request->validate([
'title' => 'bail|required|unique:posts|max:255',
'body' => 'required',
]);
In this example, if the unique rule on the title attribute fails, the max rule will not be checked. Rules will be validated in the order they are assigned.

Laravel validation required_if is not working

I have the email and password fields in which I only want them to be required if the add_user field value is 1. This is my validation:
public function rules() {
return [
'add_user' => ['required'],
'password' => ['required_if:add_user,1', 'min:8'],
'email' => ['required_if:add_user,1', 'email', 'max:255'],
];
}
This one is not working, it always errors even if the value is 0. I fill out the add_user value using jquery. I have a drop down that if the user clicked from the options, add_user value is 0, but if thru input, value is 1. Values are correct but the validation isn't working. Can someone help me figure this out?
Okay. Got it.
I added a nullable validation property and now it works.
return [
'add_user' => ['required'],
'password' => ['required_if:add_user,1', 'min:8', 'nullable'],
'email' => ['required_if:add_user,1', 'email', 'max:255', 'nullable'],
];
Hope this will help someone, someday.
We are checking two fields that are not dependent to each other means if user_id!=1 there will be some other validation on password and email so we need to check it by this way
$rules = [
'type' => 'required'
];
$v = Validator::make(Input::all(),$rules);
$v->sometimes('password', 'required|min:8', function($input) {
return $input->type == '1';
});
Here we make an instance of Validator where we checking password is required if user_id ==1 else we no need to validate it.
In Model you can do this.
public function rules(){
return [
'user_id ' => 'required'
];
}
protected function getValidatorInstance() {
$validator = parent::getValidatorInstance();
$validator->sometimes('password', 'required|min:8',
function($input) {
return $input->user_id== 1;
});
return $validator;
}

Validate whether multiple fields are unique simultaneously in laravel 5

This my request class rules.
return [
'title' => 'required|unique:event_cals,title',
'eventDate' => 'required|date|after:yesterday',
'venue' => 'required',
'time' => 'required',
'type' => 'required',
'unique:event_cals,eventDate,NULL,id,venue,$request->venue,time,$request->time'
];
I want to validate a rule like below.
'eventDate' && 'venue' && 'time' => 'unique'
There I need to check if there any row without same eventDate, venue and time altogether. Anyone knows how to declare such a rule?
This is the snapshot of db table.
Here is the possible solution:
<?php
return [
'title' => 'required|unique:event_cals,title',
'eventDate' => 'required|date|after:yesterday',
'venue' => 'required',
'time' => 'required',
'type' => 'required',
'event_date' => "unique:event_cals,event_date,NULL,id,venue,{$request->venue},time,{$request->time}",
];
I again want to highlight that; if you want that validator to work, you should make sure that the event_date and time should be correctly formatted.
An example unique check with additional wheres from our running project's update requests:
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$id = $this->route('money');
return $rules = [
'name' => "required|string|min:3|max:255|unique:moneys,name,{$id},id,deleted_at,NULL,access,public",
];
}

Resources