如何在 Laravel 中验证整数数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24588975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:46:45  来源:igfitidea点击:

How do I validate an array of integers in Laravel

phparraysvalidationlaravellaravel-4

提问by Kola

I have an array of integer like this $someVar = array(1,2,3,4,5). I need to validate $someVarto 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.

非常感谢您的帮助。

回答by Aayush Anand

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

回答by Issam Zoli

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 numericarraymethod.

编辑:此验证的最新版本不需要numericarray方法的定义。

$rules = [
    'someVar'   => 'required|array',
    'someVar.*' => 'integer',
];

回答by Lars

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

回答by Mateusz Nowak

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'

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

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

回答by Majbah Habib

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

回答by Farid Movsumov

AppServiceProvider.php

应用服务提供者.php

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

Assert.php

断言.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;
}

回答by Ruke

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