【问题标题】:laravel 5.1 required_without validationlaravel 5.1 required_without 验证
【发布时间】:2018-08-15 07:51:44
【问题描述】:

使用 laravel 5.4 我有 2 个字段: kg_bags 和 g_bags

其中至少一个必须具有大于零的正值。

如何更改所需的验证_无需这样做?

如果我将零或 -value 放在一个中而另一个中没有,我不会收到错误,这是错误的。

如果我在 kg_bags 中输入 -1 而在 g_bags 中不输入任何内容,我也不会收到错误,但代码会失败,因为它不能使用负值。

我的代码:

 if($request->kg_bags) {
        $this->validate($request,[
            "kg_bags" => "numeric"
        ]);
    }

    if($request->g_bags)
    {
        $this->validate($request,[
            "g_bags" => "numeric"
        ]);
    }

    $this->validate($request,[
        "kg_bags" => "required_without:g_bags",
        "g_bags" => "required_without:kg_bags"
    ],
    [
        'kg_bags.required_without' => 'Please fill in the number of bags you packed',
        'g_bags.required_without' => 'Please fill in the number of bags you packed',
    ]);

【问题讨论】:

    标签: validation laravel-5


    【解决方案1】:

    由于 -value 或 zero 不是您想要的值(仅是正值),您是否考虑在两列中添加最小值和最大值?

    类似

    $this->validate($request,[
            "kg_bags" => "required_without:g_bags|between:0.00,999.99",
            "g_bags" => "required_without:kg_bags|between:0.00,999.99"
        ],
        [
            'kg_bags.required_without' => 'Please fill in the number of bags you packed',
            'g_bags.required_without' => 'Please fill in the number of bags you packed',
        ]);
    

    if($request->kg_bags) {
            $this->validate($request,[
                "kg_bags" => "numeric|between:0.00,999.99"
            ]);
        }
    
        if($request->g_bags)
        {
            $this->validate($request,[
                "g_bags" => "numeric|between:0.00,999.99"
            ]);
        }
    

    希望对你有帮助

    【讨论】:

    • 谢谢,但我没有最大限制
    • 我只是试了一下看看会发生什么:在:1,999 之间,我在 kg_bags 中输入了 -1。没有收到错误,我的代码失败了,因为它不能使用负值
    • 另一件事:当我使用简单的表单提交时,min:1 验证适用于 -1,但是当我使用 ajax 时,它不会给出错误。
    • 好的,一切正常,我将 required_without 验证放在似乎可以解决问题的 numeric|min:1 之前
    • 很高兴听到这个消息:)
    猜你喜欢
    • 2014-03-26
    • 2022-01-19
    • 2021-12-09
    • 2021-03-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2020-11-20
    相关资源
    最近更新 更多