php - Laravel validate at least one item in a form array -
i have form series of numbers in array:
<input type="number" name="items[{{ $sku }}]" min="0" /> <input type="number" name="items[{{ $sku }}]" min="0" /> <input type="number" name="items[{{ $sku }}]" min="0" />
now validate there least 1 of input fields has value.
i tried following in ordercreaterequest it's passing:
return [ 'items' => 'required|array|min:1' ];
am missing something?
i think need custom validation rule following because min not elements of array.
validator::extend('check_array', function ($attribute, $value, $parameters, $validator) { return count(array_filter($value, function($var) use ($parameters) { return ( $var && $var >= $parameters[0]); })); });
you can create validatorserviceprovider , can add these lines boot method of validatorserviceprovider. need add provider providers array in config/app.php.
app\providers\validatorserviceprovider::class,
or add them top of action of controller.
at end can use in validation rules.
'items' => 'check_array:1',
note: if understand correctly works.
Comments
Post a Comment