Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4: validate checkbox at least one

I need to validate checkbox array:

<input name="cats[]" type="checkbox" value="1"> sport
<input name="cats[]" type="checkbox" value="2"> music
<input name="cats[]" type="checkbox" value="3"> business

I found "array" validation in documentation:

Validator::make( 
    [ 'cats' => Input::get('cats') ],
    [ 'cats' => 'array' ]
);

Is there any built-in way to check if at least one item checked? Also, how to check if values submitted match a given list?

like image 717
mwafi Avatar asked Nov 30 '22 01:11

mwafi


1 Answers

As of laravel 5 you can just add required rule

<input name="cats[]" type="checkbox" value="1"> sport
<input name="cats[]" type="checkbox" value="2"> music
<input name="cats[]" type="checkbox" value="3"> business

// Controller
$rules = $this->validate($request, array('cats'=>'required'));
// will do the work
like image 108
Sumeet Avatar answered Dec 04 '22 04:12

Sumeet