I have a form with 5 multiple-choice dropdown lists. When submitted, I am trying to run some validation to check that at least one item has been checked.
The code in my controller;
$input = Request::except('postcode_id'); //all user input from the form
$validator = \Validator::make(
[
$input => 'required'
]
);
if ($validator->fails())
{
print "failed";
}else{
print "passed";
}
The error I get is; Illegal offset type
. I think I might need to do a custom validator but would like to check first in case there is an easier way.
You need to use strings in your validator, not variables. Try this instead.
Custom validator itself is not too difficult. I am using it all the time for array input validation. In Laravel 5 Request I will do something like that
The first argument of
Validator::make()
is the data, and the second is an array of validation rules, which are indexed by the input names. You can userequired_without_all
to validate that at least one must be present, but it is a little verbose:Or write some code to generate the
$rules
array: