laravel 5.1 required_without validation

时间:2018-03-07 09:14:34

标签: validation laravel-5

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

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

如何更改require_without验证呢?

如果我将Zero或-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',
    ]);

1 个答案:

答案 0 :(得分:0)

由于-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"
        ]);
    }

希望有所帮助