How should I validate two-dimensional array in laravel - laravel

I am new learner of laravel.Now I have to use the laravel validation in my project.The data from the form is two-dimensional array in the follwing format:
[['product_id'=>1, 'quantity'=>2], ['product_id' => 4, 'quantity'=>5]]
I need to validate every product_id, how should I write the validation rule?
thank you!

This will validate the product_id field of each associative array within the form data.
$validator = Validator::make($request->all(), [
'*.product_id' => // your validation rule
]);

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

How to use Rule:unique with array of values compared to another column in laravel?

i have a request for array of values and i validate it with the following code
$request->validate([
'doctor_id.*' => ['required'],
'doctor_id' => [Rule::unique('project_orders')->where(function ($query) use ($student) {
$query->where('student_id', $student->id);
})],
]);
but the unique validation doesn't work and the data was inserted to the table with duplication
i want the doctor_id field to be unique with the student_id column, what should be the correct rule?
any help please ?
First you need to do validation on database level, so you will add in your migration
$table->unique(['doctor_id', 'student_id']);
This will make sure that there will be no duplication for same doctor_id and student_id values
Then in your validation layer you will add
$request->validate([
'doctor_id.*' => 'unique:project_orders,doctor_id,NULL,id,student_id,'.$student->id,
]);
You can do the validation like this
$request->validate([
'doctor_id.*' => [ Rule::unique('project_orders', 'doctor_id')->where('student_id', $student->id) ]
]);

Vue Vee validate in Laravel method unique

Laravel validate has method unique but vue-vee validate doesn't has it. How can i use unique in vue-vee validate? (I mean v-validate='')
For example in laravel:
return $validator = [
'title' => 'required|unique:rubrics,title',
];
In advance thanks for help.

Validate that each comma separated value exists in database?

Using Laravel 5.4, my users have an auto-complete helper to put values into an input. I want to validate that each of the values exists in the database when inserting.
Inputted value for "unit" : "12,13,14"
How do I check that, unit "12" and unit "13" and unit "14" exist in the database before doing the insert?
$units = array_filter(array_unique(explode(",", $request->unit)));
// input "12,13,14" becomes [12,13,14]
$this->validate($request,[
'unit' => 'required|exists:units.id,'.$units,
]);
Do I have to use a custom validation rule, or does laravel have something handy like 'required|existsAllValuesInThisArray' sorta thing? Haven't found anything in documentation about it.
I also found this, but it's for like multiple select fields or checkboxes sorta thing from the looks of it.
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);
Update : I ended up using javascript to split the input into an array before sending it off for processing. So my input name became "units[]" instead of "units"
Try the following:
$this->validate($request,[ 'unit.*' => 'required|exists:units.id,'.$units, ]);
Since $units is an array, the rule unit.* should check for each element of the array.

Validate values in form arrays

I've a post value members and members is an array of values. How can I validate that one specific value is not in the list?
Example
$this->validate($request, [
'member_ids' => 'required|min:1|not_in:4',
]);
Thanks!
Laravel 5.2 has array validation.
So try this:
$this->validate($request, [
'member_ids.*' => 'required|not_in:4',
]);
I am not sure what it is you would like to achieve, but I think you can find a solution from here: http://laravel.io/forum/11-12-2014-how-to-validate-array-input

Resources