How to check array of integers in Laravel

I have an array of integers like $someVar = array(1,2,3,4,5) . I need to check $someVar to make sure every element is numeric. How can i do this?

I know that for the case of one significant variable, the check rule will be something like this $rules = array('someVar'=>'required|numeric') . How can I apply the same rule to every element of the $someVar ?

Thank you for help.

+6
source share
7 answers
 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') 
+12
source

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

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

or

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

Start by 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 the attribute is not an array or if one value is not a numeric value. Then add the message to `app / lang / en / validation.php '

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

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

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

There is only an “array” check, which ensures that this 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

+1
source

You can add custom rules to check the value of an integer array type

Just open the file

 /resources/lang/en/validation.php 

Add a custom message before the “received” 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 custom validation in the download 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 numericarray to check the value of an integer array type

 $this->validate($request, [ 'field_name1' => 'required', 'field_name2' => 'numericarray' ]); 
+1
source

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; } 
0
source

Source: https://habr.com/ru/post/971807/


All Articles