簡體   English   中英

如何在Laravel中驗證整數數組

[英]How do I validate an array of integers in Laravel

我有一個像這樣的整數數組$someVar = array(1,2,3,4,5) 我需要驗證$someVar以確保每個元素都是數字,該怎么做?

我知道對於單值變量,驗證規則將類似於$rules = array('someVar'=>'required|numeric') 如何將相同的規則應用於數組$someVar每個元素?

非常感謝您的幫助。

現在,laravel具有在數組元素上設置條件的選項。 無需為驗證int數組之類的簡單事情編寫自己的驗證器。 使用它(如果在控制器中使用)-

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

要么

$this->validate($request, [
    'someVar' => 'array',
    'someVar.*' => 'int'
]);
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',
];

在Laravel 5中,您可以使用.*檢查數組中的元素。 對您而言,這意味着:

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

從添加新的驗證屬性開始

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

如果attribute不是數組或一個值不是數字值,則該函數將返回false。 然后將消息添加到“ app / lang / en / validation.php”

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

您可以為數組的整數類型值檢查添加自定義規則

剛打開文件

/resources/lang/en/validation.php

在文件中的“已接受”消息之前添加自定義消息。

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

只有“數組”驗證可確保該值是一個數組,但是對於您的特定情況,您將必須創建一個自定義過濾器:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM