Laravel 5.4 different value validation rule in array - laravel

I have an input with an array of entities with an ID which must be unique
I've tried this:
'authors.*.id' => 'different:authors.*.id'
But it says 'The authors.0.id and authors.0.id must be different'
So what is a right way to validate this?

You want to use distinct rule.
When working with arrays, the field under validation must not have any duplicate values.
'foo.*.id' => 'distinct'

Related

Laravel Validation of Array

Laravel Help with validation
Hi everyone I want to validate an array of a foreign key to make sure it exists I am doing
'activities' => 'required|array',
'activities.*' => 'exists:admin_activities,id',
but it's skipping this validation
any solution for this
You can validate each element of an array in Laravel with "dot notation" to For example, to validate that each id in a given array input field is exists in admin_activities , you may do the following:
'activities.*.id' => 'required|exists:admin_activities,id',

Laravel validate array values contained in list

I am passing an array in a request to my api. Each value within the array must be within a pre-defined list.
If my list is: name,description,title
name, title //valid
different, title //invalid
I tried array|in:name,description,title but I think for that I can only pass a string.
Can I do this without using a custom rule?
Validate each string in the array:
'values.*' => 'string|in:name,title,description'
Have a look at "Validating Nested Array Input"
If I understand you correctly your validation rules should be (untested)
[
'*.name' => 'required|string',
'*.description' => 'required|string',
]
Maybe you also want to exclude unvalidated Keys

Laravel Validation - Individual rule and individual field problem

i have a field in Laravel with the name "company_url". and its stored like this.
$post['company_url'] = "http://example.org";
and then i have a rule string which i have stored for validation which must be applied on this individual field. which is stored like this
$post['rule'] = "required|max:24";
now i am trying this code to get validation errors. which is not working.
$validator = Validator::make([$post['name']], [$post['rules']]);
tell me what is the way to get errors on this validation?
The data you're passing to make() is in incorrect format. It should be in key-value pair format.
Also I don't know where the $post['name'] coming from. I assume it is company_url not name.
$post['rules'] is also undefined. It should be $post['rule']
The following should work:
$validator = Validator::make(['company_url' => $post['company_url']], ['company_url' => $post['rule']]);

Laravel validation rule for value based on a table, but can be zero

How can I create a validation rule to a value what can be 0 but if not, then must be based on an existing value of a table?
Is there a built-in solution or any best practice?
In this case I would go with a nullable field instead of having 0, and then the validation will be:
'field' => 'nullable|exists:table,column'
you could also use
'field' => 'sometimes|exists:table,column'

How to check a field value is must be a string when it filled out in laravel 5.4 validator

I have a field called middle name which is an optional field. I want to check this field if it has some value otherwise I dont want to check this field value.
How can I do this in laravel 5.4 validator.
String is an object type its not primitive type like integer or char. I didn't check how laravel handles this validation but based on documentation you have to put nullable. Probably because string returns an object with empty attribute. so #Gaurav Gupta's answer not going to work.
The field under validation must be a string. If you would like to
allow the field to also be null, you should assign the nullable rule
to the field.
'middle_name' => 'nullable|string'
you can just simply write
'middle_name' => 'nullable|string',
in your validation array it check only when some value exits .Means it not required value

Resources