public function rules()
{
return [
'vote' => 'array|max:5',
];
}
Add array size check to validation
hi, I have a voting system where each user has to vote vote for a number of choices out of a given set of choices, i.e. select 5 out of 10 people by checking on 5 checkboxes coressponding to 5 pepople that user wants to vote for. So my system has to check to make sure the user checks exactly on 5 checkboxes, no more, no less before saving the votes into the database.
I've been trying to figure out how to get this to work with Laravel's validation capability with no luck. Here is what my request looks like:
class SubmitVoteRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
$votes = Request::get('vote');
$count = count($votes);
$limit = 5;
$rules[$count] = $limit;
return $rules;
}
}
Here is part of the controller code:
public function StoreVote(Requests\SubmitVoteRequest $request){
$votes = Request::get('vote');
//Something to perform if validation passes
}
And here is the top of the error page I get when I try to submit:
ErrorException in Validator.php line 424: Invalid argument supplied for foreach()
in Validator.php line 424
at HandleExceptions->handleError('2', 'Invalid argument supplied for foreach()', '/var/www/html/tchc-eval/vendor/laravel/framework/src/Illuminate/Validation/Validator.php', '424', array('attribute' => '0', 'rules' => '5')) in Validator.php line 424
....
I am sure there is something in my SubmitVoteRequest code but can't figure out how.
Please help
Thank you
return ['vote.size'=>'Required number of votes must be :size'];
return ['vote.required'=>'You must vote for someone'];
Hey you return two things in this function ! Do this :
return [
'vote.size'=>'Required number of votes must be :size',
'vote.required'=>'You must vote for someone',
];
Please or to participate in this conversation.