如何在Laravel中验证整数数组

34

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

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

非常感谢您的帮助。

7个回答

89

现在 Laravel 已经支持在数组元素上设置条件。对于像验证整数数组这样的简单任务,无需编写自己的验证器。如果在控制器中使用,则可以使用以下代码:

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

需要翻译的内容为 "

or

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

1
这是关于Laravel 5的最简单和最新的答案。 - Stetzon
Laravel文档明确说明整数验证规则...“此验证规则不验证输入是否为“整数”变量类型,仅验证输入是否为包含整数的字符串或数字值。” - omarjebari

39
Validator::extend('numericarray', function($attribute, $value, $parameters)
{
    foreach($value as $v) {
         if(!is_int($v)) return false;
    }
    return true;
});

使用它

$rules = array('someVar'=>'required|array|numericarray')

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

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

“is_array” 也应该在其中。 - Jarek Tkaczyk
2
@deczo是正确的,关于使用is_array。所以,我已经修改了@Issam Zoli的代码如下: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); });谢谢大家。 - Kola
你应该使用 is_numeric 而不是 is_int - Mateusz Nowak
在这个问题的范围内,如果您有混合类型,请使用 is_numeric。如果您只有整数类型,请使用 is_int。 - Issam Zoli
use Illuminate\Support\Facades\Validator; - Pathros
请查看Aayush Anand的回答,因为它更为最新且不需要额外的验证器扩展。 - Danon

11
在 Laravel 5 中,您可以使用 .* 检查数组中的元素。对于您来说,这意味着:
$rules = array('someVar'   => 'required|array',
               'someVar.*' => 'integer')

6

首先,需要添加一个新的验证属性

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

该函数如果属性不是一个数组或者其中一个值不是数值类型,将返回false。然后将该信息添加到“app/lang/en/validation.php”文件中。
"numeric_array"        => "The :attribute field should be an array of numeric values",

3

您可以添加自定义规则,用于检查数组中整数类型值的有效性

只需打开文件即可

/resources/lang/en/validation.php

在文件中的“accepted”消息之前添加自定义消息。
'numericarray'         => 'The :attribute must be numeric array value.',
"accepted"             => "The :attribute must be accepted.",

现在打开文件。
/app/Providers/AppServiceProvider.php

然后在启动函数中添加自定义验证。

public function boot()
{

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

}

现在,您可以使用numericarray来检查数组的整数类型值。
$this->validate($request, [
            'field_name1' => 'required',
            'field_name2' => 'numericarray'
        ]);

1

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接