How do I validate an array of integers in Laravel
arrays, laravel, laravel-4, php, validation
Solution
Validator::extend('numericarray', function($attribute, $value, $parameters)
{
foreach($value as $v) {
if(!is_int($v)) return false;
}
return true;
});
Use it
$rules = array('someVar'=>'required|array|numericarray')
Edit: Up to date version of this validation would not require the definition of `numericarray` method.
$rules = [
'someVar' => 'required|array',
'someVar.*' => 'integer',
];
Problem
I have an array of integer like this `$someVar = array(1,2,3,4,5)`. I need to validate `$someVar` to make sure every element is numeric.How can I do that? I know that for the case of a single valued variable, the validation rule would be something like this `$rules = array('someVar'=>'required|numeric')`. How can I apply the same rule to every element of the array `$someVar`? Thanks a lot for helping.