How do I validate an array of integers in Laravel

27,494

Solution 1

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',
];

Solution 2

Now laravel has option to set condition on array elements. No need to write your own validator for simple things like validation int array. Use this (if using in controller)-

$validator = \Validator::make(compact('someVar'), [
    'someVar' => 'required|array',
    'someVar.*' => 'integer'
]);
$this->validateWith($validator);

or

$this->validate($request, [
    'someVar' => 'array',
    'someVar.*' => 'int'
]);

Solution 3

In Laravel 5 you can check the elements in an array by using .*. For you this would mean:

$rules = array('someVar'   => 'required|array',
               'someVar.*' => 'integer')

Solution 4

Start with adding a new validation attribute

Validator::extend('numeric_array', function($attribute, $values, $parameters)
{
    if(! is_array($values)) {
        return false;
    }

    foreach($values as $v) {
        if(! is_numeric($v)) {
            return false;
        }
    }

    return true;
});

The function will return false if attribute is not an array or if one value is not a numeric value. Then add message to `app/lang/en/validation.php'

"numeric_array"        => "The :attribute field should be an array of numeric values",

Solution 5

You can add custom rules for integer type value check of array

Just open file

/resources/lang/en/validation.php

Add the custom message before "accepted" message in the file.

'numericarray'         => 'The :attribute must be numeric array value.',
"accepted"             => "The :attribute must be accepted.",

Now Open the file

/app/Providers/AppServiceProvider.php

and then add the custom validation in boot function.

public function boot()
{

    $this->app['validator']->extend('numericarray', function ($attribute, $value, $parameters)
    {
        foreach ($value as $v) {
            if (!is_int($v)) {
                return false;
            }
        }
        return true;
    });

}

Now you can use the numericarray for integer type value check of array

$this->validate($request, [
            'field_name1' => 'required',
            'field_name2' => 'numericarray'
        ]);
Share:
27,494

Related videos on Youtube

Kola
Author by

Kola

Updated on July 09, 2022

Comments

  • Kola
    Kola almost 2 years

    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.

  • Jarek Tkaczyk
    Jarek Tkaczyk almost 10 years
    is_array should be there too
  • Kola
    Kola almost 10 years
    @deczo is right, about using is_array. So, I have modified @Issam Zoli's code as follows: Validator::extend('numericarray', function($attribute, $value, $parameters) { if(is_array($value)) { foreach($value as $v) { if(!is_int($v)) return false; } return true; } return is_int($value); }); Thanks everyone.
  • Mateusz Nowak
    Mateusz Nowak over 9 years
    and you should use is_numeric instead of is_int probably
  • Issam Zoli
    Issam Zoli over 9 years
    In the scope of the question is_int does the job if you have mixed types use is_numeric.
  • Pathros
    Pathros almost 7 years
    use Illuminate\Support\Facades\Validator;
  • Stetzon
    Stetzon almost 7 years
    This is the easiest and most up to date answer for Laravel 5
  • Danon
    Danon over 6 years
    Check out answer of Aayush Anand because it's more up to date and doesn't require additional validator extends.
  • omarjebari
    omarjebari over 4 years
    The Laravel documentation clearly states for the integer validation rule ... 'This validation rule does not verify that the input is of the "integer" variable type, only that the input is a string or numeric value that contains an integer.'