Laravel Validation of Array - laravel

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',

Related

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

validation and duplicate update() in laravel

In my form Voiture, I want to do a validation system for the fields immatriculation and num_vehicule
Here i an overview:
If I edit the first recording ie the value of the field num_vehicule 000001 per 0000032and that I validate, I have an error message because the value of the field immatriculation already exists.
I don't understand the problem... Do you have an idea please?
'immatriculation' => 'required|string|max:15|min:6|unique:voitures,immatriculation',
'num_vehicule' => 'required|string|max:6|min:6|unique:voitures,num_vehicule',
'fk_modele' => 'required'
Thank you
Use
'immatriculation' => 'string|max:255|unique:voitures,immatriculation,' . $this->voitures->id,
The syntax is
'input_field' => 'unique:<table name>,<column name for this input field>, <unique id>, <unique id column in table>';

Laravel 5.4 different value validation rule in array

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'

unique between 2 columns

i need to create a validation to my table "candidate_knowledges", basically in this table it accepts to columns (candidate_id, software_id), i cannot let create user_id and software more then one. but i think my validation is wrong or im not doing it right.
What im trying to say in validation is that can only exist one software_id and one candidate_id on the table, this way the candidate dont have duplicate entries.
Ex: 'software_id' => 'required|integer|unique:candidate_knowledges,candidate_id,'.$candidate->id,
Here is a way to allow only one software for each candidate:
$rules = [
'software_id' =>
'required',
'integer',
'min:1',
Rule::unique('candidate_knowledges')->where('candidate_id', $candidate->id),
],
];
In general I would suggest using fluent validation syntax (it's match easier to use and update), but if you can't (laravel < 5.3 or any other reasons):
'software_id' => 'required|integer|unique:candidate_knowledges,NULL,NULL,candidate_id,' . $candidate->id,
Hope it helps.

laravel validation check field equal something

how to check a field is exactly equal to a string or number
I want to check a field named course_id is equal a field of database id in course database. now I want to check if course_id is equal to id.
You can validate it by using the exists validation rule:
$validationRules = ['course_id' => 'exists:course,id'];
create rule as below and use validator on input
$rules = array(
'id' => 'exists:your_table_name'
);
for more help
https://laravel.com/docs/5.2/validation#rule-exists

Resources