Validate values in form arrays - laravel

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

Related

Laravel validation for accepting only predefined values from api

Basically I wrote an api in laravel, The api should return a validation error if any of the key has wrong values (spelling mistakes,extra space). To make more clarity, in the web interface these key values are from select boxes . so users do not get to type anything.
First consider using in_array function for every inputs. I think that works. But i would like to know if there is anything for laravel specific.
something like
'email' => 'required | email| 'sandy#stackoverflow.com'
to make it ease. I could not find it unfortunately. It seems not that hard.
I believe you can achieve this with in, for example:
$rule = [
'email' => 'in:sandy#stackoverflow.com',
];
Or you could try it including the Rule namespace as described in the docs here
use Illuminate\Validation\Rule;
Validator::make($data, [
'zones' => [
'required',
Rule::in(['first-zone', 'second-zone']),
],
]);

How should I validate two-dimensional array in 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
]);

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.

Laravel validation of spezific field/value

i have a form field with two options:
{!! Form::select('pet',array('1'=>'Dog', '2'=>'Cat') ,null,['class'=>'form-text']) !!}
is there any way to validate this values via $this->validate($request, [...]) or do i have to do this via if statement?
thanks for any sugestions.
You cant prevent people edit the data
You have to validate the data once you have the request in you server
Could try like this:
$this->validate(request(), [
'pet' => 'required|in:1,2'
]);
so only if pet value is 1 or 2 will pass.
not tested but should work

Resources