Kola Kola - 1 year ago 90
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
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

Thanks a lot for helping.

Answer Source
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')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download