Laravel 5 - 根据需要验证数组,但允许传递空数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43635412/
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 15:51:28  来源:igfitidea点击:

Laravel 5 - validate array as required, but allow an empty array to be passed

phplaravelvalidationlaravel-5laravel-5.4

提问by Rafael K.

I'm validating a request in Laravel 5.4 with the validator, see the documentation: https://laravel.com/docs/5.4/validation#validating-arrays

我正在使用验证器验证 Laravel 5.4 中的请求,请参阅文档:https://laravel.com/docs/5.4/validation#validating-arrays

Basically, it's this code in the Controller:

基本上,它是控制器中的这段代码:

public function createSomeResource(Request $request)
{
    $this->validate($request, [
        'items' => 'required',
    ];
    ...
}

I would like to require the presence of the field "items" and this code does it, but the problem is that the validation fails when the "items" field is an empty array, i.e.

我想要求字段“items”的存在,这段代码做到了,但问题是当“items”字段是空数组时验证失败,即

{
    "fields": []
}

, which is an undesired behavior. I know that's the documented behavior of the "required" parameter but I don't see any "clean" workaround. I tried also:

,这是一种不受欢迎的行为。我知道这是“必需”参数的记录行为,但我没有看到任何“干净”的解决方法。我也试过:

public function createSomeResource(Request $request)
{
    $this->validate($request, [
        'items' => 'required_unless:items,[]',
    ];
    ...
}

but it fails as well, probably because the documentation says that it works with a different field after the "required_unless" clause, but I'm not totally sure about it.

但它也失败了,可能是因为文档说它在“required_unless”子句之后使用不同的字段,但我不完全确定。

Could you suggest me a way to require the presence of the field "items" without forbidding the empty array?

你能给我建议一种方法来要求字段“项目”的存在而不禁止空数组吗?

EDIT: another "obvious" approach that has come to my mind is to use the "present|array" rule and it almost does what I want, but unfortunately, an empty string passes that validation rule as well, which is maybe a bug in Laravel, maybe not - I opened an issue for it on the Laravel github repository: https://github.com/laravel/framework/issues/18948

编辑:我想到的另一种“明显”方法是使用“present|array”规则,它几乎可以满足我的要求,但不幸的是,一个空字符串也通过了该验证规则,这可能是Laravel,也许不是 - 我在 Laravel github 存储库上为它打开了一个问题:https: //github.com/laravel/framework/issues/18948

回答by silentavt

Try this:

尝试这个:

public function createSomeResource(Request $request)
{
    $this->validate($request, [
        'items' => 'present|array',
    ];
    ...
}

回答by ImLeo

Try:

尝试:

public function createSomeResource(Request $request)
{
    $this->validate($request, [
        'items' => 'required|array|min:1',
    ];
    ...
}

From Laravel doc:

来自 Laravel 文档:

min:value The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.

min:value 被验证的字段必须有一个最小值。字符串、数字、数组和文件的计算方式与大小规则相同。

回答by Kabelo Tooka

Here we go buddy...

来吧,伙计……

public function createSomeResource(Request $request)
{
    $validate_us_pls = [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ];


    if( !empty($request->get('items')) ){
        $validate_us_pls['items'] = 'required';
    }

    $this->validate($request, $validate_us_pls);

}

回答by Roberto Lyra

Your problem is because you are not sending the request as JSON. Make sure that you have the settings on POSTMAN to send your request as JSON.

您的问题是因为您没有将请求作为 JSON 发送。确保您在 POSTMAN 上进行了设置以将您的请求作为 JSON 发送。

回答by Piotr Jankiewicz

Maybe this will be usefull?

也许这会有用?

size in array uses count

数组中的大小使用计数

 'ids'=>'present|array|size:1'

or this

或这个

'users' => 'required|array|between:2,4'

回答by sumit

probably this should work

可能这应该有效

public function rules()
{
   return [
    "items"    => "required|array|min:0",
    "items.*"  => "required|string|distinct|min:0",
  ];
}