Laravel验证:required_with_all条件始终通过 [英] Laravel Validation: required_with_all condition always passing

查看:1006
本文介绍了Laravel验证:required_with_all条件始终通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 laravel验证文档:

required_with_all:foo,bar,... 仅当所有其他指定的字段都存在时,验证字段才必须存在.

required_with_all:foo,bar,... The field under validation must be present only if all of the other specified fields are present.

这是我的考试:

    Route::get('/test/{param}', function($param) {
        $screenRules = array(
            'foo' => 'string',
            'param' => 'required_with_all:foo',

        );

        $validator = Validator::make(array('param' => $param), $screenRules);
        if($validator->fails())
        {
            echo '<pre>';
            print_r($validator->errors());
            echo '</pre>';
            die('Dying now!');
        }
        else
        {
            echo 'All Good!';
            die('Dying now!');
        }
    });

我希望由于我没有通过foo,因此该测试将失败.但是,当我调用自己的网址时,该测试通过了: mysite.com/test/mytest

I would expect that since I am not passing foo, this test should fail. But this test passes when I call my url: mysite.com/test/mytest

类似地,如果您反转角色,就像这样

Similarly, if you reverse the role, like so

'param' => 'required_without_all:foo',

并通过foo作为输入

array('param' => $param, 'foo' => 'bar')

我希望因为存在foo,所以不能将param作为输入.但是测试仍然通过.

I would expect that since foo is present, you cannot have param as input. But the test still passes.

在两种情况下,我都应该看到错误.那么这是怎么回事?

In both the cases, I should be seeing errors. So what is going on here?

我也知道我的验证功能没有错,因为我的其他验证工作正常.例如.添加这样的条件:

I also know that my validation functions are not wrong, because my other validation works. E.g. add a condition like so:

'param' => 'required_with_all:foo|numeric',

它确实会引发错误The param must be a number.

推荐答案

我终于找到答案了.我误解了整个required_if概念.这个答案将帮助任何陷入困境的人.关键是要一遍又一遍地阅读laravel文档;)

I finally figured out the answer. I misunderstood the whole required_if concept. This answer will help anyone who ever gets stuck in here. The key is reading the laravel documentation again and again ;)

对于required_with_all,它说:仅当所有其他指定的字段都存在时,验证字段才必须仅出现.

For required_with_all it says: The field under validation must be present only if all of the other specified fields are present.

例如param => required_with_all:foo,bar,... 表示当foo和bar都存在而param不存在时,将发生验证错误.

E.g. param => required_with_all:foo,bar,... means that when foo and bar, both are present, and param is not present, then the validation error will occur.

我误解了它,所以如果param在那儿,那么它也需要foobar在那儿.就像我们刚刚看到的那样,相反.

I misunderstood it such that if param is there, then it requires foo and bar to be there too. Its the other way round though as we just saw.

如果您需要以if param is present, foo must be present身份进行验证,请对foo使用 required_with 验证规则,如下所示:

If you need to do a validation as if param is present, foo must be present use the required_with validation rule on foo like so:

'param' => 'required_with:foo',
'foo' => 'required_with: param

这将确保如果fooparam中存在任何一个,则它们也将要求另一个也存在.

This will ensure that if foo or param, anyone of them is present, they will require the other to be present too.

第一个测试用例

'param' => 'required_with_all:foo'

我通过了以下输入

array('param' => $param)

测试用例失败,因为输入中没有foo,没有触发验证,因为该规则仅在foo存在时检查.因此,只需传递foo作为输入,而不是传递param作为输入,您将看到存在foo时需要param的错误.

The test case failed because foo not being in the input, did not trigger the validation because the rule says only check when foo is present. So instead of passing param as input, simply pass foo as input and you will see the error that param is required when foo is present.

第二个测试用例

'param' => 'required_without_all:foo',

并像这样传递输入:

array('param' => $param, 'foo' => 'bar')

此验证不起作用,因为它说仅当foo不存在时才需要param.但是,在我的输入中,我已经传递了foo.删除fooparam,您将看到以下错误:如果不存在foo,则需要param.

This validation did not work because it says param is only required when foo is not present. However, in my input I have passed foo. Remove both foo and param and you shall see the error that when foo is not present, param is required.

我有多傻!

这篇关于Laravel验证:required_with_all条件始终通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆