繁体   English   中英

Laravel “最少和最多”必填字段

[英]Laravel "At Least and At Most" one field is required

我有四个不同的字段 a、b、c 和 d。 我只想填满其中一个。 我该怎么做?

我在 Laravel 文档中发现了required_without_all:foo,bar,...但这似乎至少允许一个字段。 我也可以填充其他人。 我只想填写一个字段,其中一个应该是必需的。

这是我到目前为止所做的,但我可以将这些字段一起发送。

public function rules()
{
    return [
        "isFlat" => "required_without_all:isDetachedHouse,isTerrace,isSemiDetached",
        "isDetachedHouse" => "required_without_all:isFlat,isTerrace,isSemiDetached",
        "isTerrace" => "required_without_all:isFlat,isDetachedHouse,isSemiDetached",
        "isSemiDetached" => "required_without_all:isFlat,isDetachedHouse,isTerrace",
        "finishQuality" => [
            "required",
            Rule::in(["luxury", "standard", "economy"])
        ],
    ];
}

尝试这个

                $validations = $request->validate([
            'text1' => [
                'bail',
                function ($attribute, $value, $fail) {
                    if (request()->filled('text2') && request()->filled("text3")) {
                        return $fail('Only 1 of the three is allowed');
                    }
                }
            ],
            "text2" => [
                'bail',
                function ($attribute, $value, $fail) {
                    if (request()->filled('text1') && request()->filled("text3")) {
                        return $fail('Only 1 of the three is allowed');
                    }
                }
            ],
            "text3" => [
                'bail',
                function ($attribute, $value, $fail) {
                    if (request()->filled('text1') && request()->filled("text2")) {
                        return $fail('Only 1 of the three is allowed');
                    }

                    if (!request()->filled('text1') && !request()->filled("text2") && !request()->filled($attribute)) {
                        return $fail('You must fill one');
                    }
                }
            ],
        ]);

替换text1text2text3

当然,您可以在function之后为每个text添加更多验证。 您可以验证是字符串还是数字等...例如在 function 之后写入“size:3” 如果您写入单个字符,这将返回错误

例子:

//validation
'text1' => [
                'bail',
                function ($attribute, $value, $fail) {
                    if (request()->filled('text2') && request()->filled("text3")) {
                        return $fail('Only 1 of the three is allowed');
                    }
                },
               "size:3"
            ],
// I fill form with these values
text1[value="q"]
text2[value=null]
text3[value="hey"]

将返回错误:

  • text1 必须是 3 个字符。
  • 三项中只允许一项

看起来您有一个表单,其中包含四种类型住房的四个复选框。 如果您需要提供其中一个选项,为什么不使用单选按钮或 select?

<label>
    <input type="radio" name="housingtype" value="flat">
    flat
</label>
<label>
    <input type="radio" name="housingtype" value="detachedHouse">
    detached house
</label>
<label>
    <input type="radio" name="housingtype" value="terrace">
    terrace
</label>
<label>
    <input type="radio" name="housingtype" value="semiDetached">
    semi-detached
</label>

或者拨打 select:

<select name="housingtype">
    <option value="flat">flat</option>
    <option value="detachedHouse">detached house</option>
    <option value="terrace">terrace</option>
    <option value="semiDetached">semi-detached</option>
</select>

然后验证应该是这样的:

[
    'housingtype' => 'required|in:flat,detachedHouse,terrace,semiDetached',
    // ...
]

一个额外的好处:现在添加一种住房类型是微不足道的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM