Kola Kola - 1 month ago 13
PHP Question

How do I validate an array of integers in Laravel

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.

Answer
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|numericarray')