Laravel 4: validate checkbox at least one
laravel, laravel-4, php, validation
Solution
You can use min:value to validate a numeric value and you can also use it to validate an array's size.
Validator::make(
[ 'cats' => Input::get('cats') ],
[ 'cats' => 'min:1' ]
);
Examples:
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 1']);
$result = $validator->fails(); // returns false
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 2']);
$result = $validator->fails(); // returns false
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 4']);
$result = $validator->fails(); // returns true
Problem
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?