32

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.

SUB0DH
  • 5,130
  • 4
  • 29
  • 46
Kola
  • 509
  • 1
  • 7
  • 12

7 Answers7

85

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'
]);
Aayush Anand
  • 1,104
  • 1
  • 10
  • 14
  • 1
    This is the easiest and most up to date answer for [Laravel 5](https://laravel.com/docs/5.4/validation#validating-arrays) – Stetzon Jul 19 '17 at 19:16
  • 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.' – omarjebari Dec 30 '19 at 13:59
37
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',
];
N69S
  • 16,110
  • 3
  • 22
  • 36
Issam Zoli
  • 2,724
  • 1
  • 21
  • 35
  • `is_array` should be there too – Jarek Tkaczyk Jul 05 '14 at 19:49
  • 2
    @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. – Kola Jul 06 '14 at 19:16
  • and you should use `is_numeric` instead of `is_int` probably – Mateusz Nowak Dec 29 '14 at 15:07
  • In the scope of the question is_int does the job if you have mixed types use is_numeric. – Issam Zoli Dec 29 '14 at 15:17
  • `use Illuminate\Support\Facades\Validator;` – Pathros May 14 '17 at 03:10
  • Check out answer of Aayush Anand because it's more up to date and doesn't require additional validator extends. – Danon Nov 22 '17 at 15:54
11

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')
Lars
  • 183
  • 1
  • 8
6

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",
Mateusz Nowak
  • 4,021
  • 2
  • 25
  • 37
3

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'
        ]);
Majbah Habib
  • 8,058
  • 3
  • 36
  • 38
1

There is only the 'array' validation which ensures that the value is an array, but for your specific case you will have to create a custom filter:

Laravel 3: http://three.laravel.com/docs/validation#custom-validation-rules

Laravel 4: http://laravel.com/docs/validation#custom-validation-rules

Ruke
  • 141
  • 5
1

AppServiceProvider.php

Validator::extend('integer_array', function($attribute, $value, $parameters)
{
    return Assert::isIntegerArray($value);
});

Assert.php

/**
 * Checks wheter value is integer array or not
 * @param $value
 * @return bool
 */
public static function isIntegerArray($value){
    if(!is_array($value)){
        return false;
    }

    foreach($value as $element){
        if(!is_int($element)){
            return false;
        }
    }

    return true;
}
Farid Movsumov
  • 12,350
  • 8
  • 71
  • 97