【问题标题】:Laravel "At Least and At Most" one field is requiredLaravel“至少和最多”一个字段是必需的
【发布时间】:2021-10-21 21:41:34
【问题描述】:

我有四个不同的字段 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"])
        ],
    ];
}

【问题讨论】:

  • 你尝试了什么?分享您的$validation
  • 我添加了代码@S.Wasta
  • 你试过required_without吗?比如isFlat => required_without: isDetachedHouse, isTerrace等等……这个的定义是:The field under validation must be present and not empty only when any of the other specified fields are empty or not present
  • 是的,required_without 不起作用。仅当存在超过 2 个字段时才有效。我只想允许四个字段之一。而且它必须是必需的。
  • 这需要我一段时间,必须启动一个项目并制作一个表格来复制它。如果你想看看这个帖子harishtoshniwal.com/blog/2018/…是你需要的吗? n 之一必须填写

标签: php laravel validation laravel-8


【解决方案1】:

试试这个

                $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 添加更多验证。您可以验证是字符串还是数字等...例如,在函数 write "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 个字符。
  • 三个中只允许一个

【讨论】:

  • ¿它有效吗?
  • 抱歉,我一直在进行其他验证。今天会尝试,我会发表评论。
【解决方案2】:

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

<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 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',
    // ...
]

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

【讨论】:

  • 谢谢。我理解这种方法。但不能用这个。
猜你喜欢
  • 2016-06-29
  • 2014-06-17
  • 2011-07-21
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2020-08-20
  • 2023-03-10
相关资源
最近更新 更多