如何在Laravel中验证整数数组

时间:2023-01-26 14:38:29

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?

我有一个像$ someVar = array(1,2,3,4,5)这样的整数数组。我需要验证$ someVar以确保每个元素都是数字。我怎么能这样做?

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?

我知道对于单值变量的情况,验证规则将类似于$ rules = array('someVar'=>'required | numeric')。如何将相同的规则应用于数组$ someVar的每个元素?

Thanks a lot for helping.

非常感谢您的帮助。

7 个解决方案

#1


14  

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

#2


22  

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)-

现在,laravel可以选择在数组元素上设置条件。无需为验证int数组这样的简单事物编写自己的验证器。使用此(如果在控制器中使用) -

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

or

要么

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

#3


5  

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

在Laravel 5中,您可以使用。*检查数组中的元素。对你来说这意味着:

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

#4


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'

如果attribute不是数组或者一个值不是数值,则该函数将返回false。然后将消息添加到`app / lang / en / validation.php'

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

#5


2  

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

现在,您可以使用numericarray进行数组的整数类型值检查

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

#6


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 3:http://three.laravel.com/docs/validation#custom-validation-rules

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

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

#7


0  

AppServiceProvider.php

AppServiceProvider.php

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

Assert.php

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

#1


14  

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

#2


22  

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)-

现在,laravel可以选择在数组元素上设置条件。无需为验证int数组这样的简单事物编写自己的验证器。使用此(如果在控制器中使用) -

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

or

要么

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

#3


5  

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

在Laravel 5中,您可以使用。*检查数组中的元素。对你来说这意味着:

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

#4


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'

如果attribute不是数组或者一个值不是数值,则该函数将返回false。然后将消息添加到`app / lang / en / validation.php'

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

#5


2  

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

现在,您可以使用numericarray进行数组的整数类型值检查

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

#6


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 3:http://three.laravel.com/docs/validation#custom-validation-rules

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

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

#7


0  

AppServiceProvider.php

AppServiceProvider.php

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

Assert.php

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