为什么Laravel的required_without_all验证不能识别我的参数?

时间:2017-04-29 13:08:28

标签: laravel validation laravel-5.4

如果其他两个输入为空,我想验证输入是否仅 。所以我正在使用 required_without_all 验证规则。

根据Required Without All的Laravel 5.4文档:

  

验证字段必须存在且仅当所有其他指定字段都不存在时才为空。

但是根据Laravel的说法,我认为我不知道现在意味着什么,因为我认为这意味着必须填写输入。

当我运行测试时,将用户名留空,无论firstname和lastname输入是填充还是空,它总是返回以下错误:

  

如果没有名字/姓氏,则需要用户名字段。

这是我在控制器上的验证:

public function test()
{
    $this->validate(request(), [
        'username' => 'required_without_all: firstname, lastname',
    ]);

    return redirect('/');
}

2 个答案:

答案 0 :(得分:1)

代码有问题。 firstname和lastname参数上不应该有任何空格。代码应该是:

'username' => 'required_without_all:firstname,lastname',

而不是:

'username' => 'required_without_all: firstname, lastname',

所以required_without_all现在工作正常。对不起,伙计们,我更新了问题,以便符合实际问题。

答案 1 :(得分:0)

这是方法定义:

``
    /**
     * Validate that an attribute exists when all other attributes do not.
     *
     * @param  string  $attribute
     * @param  mixed   $value
     * @param  mixed   $parameters
     * @return bool
     */
    protected function validateRequiredWithoutAll($attribute, $value, $parameters)
    {
        if ($this->allFailingRequired($parameters)) {
            return $this->validateRequired($attribute, $value);
        }

        return true;
    }
```

如您所见,验证在没有给出所有其他属性时传递。

相关问题