【问题标题】:Validation on checkbox where one one checkbox must be checked in laravel验证复选框,其中一个复选框必须在 laravel 中选中
【发布时间】:2020-06-24 14:10:25
【问题描述】:

我现在有以下复选框,希望在必须选中一个复选框的复选框中进行验证。但我不知道该怎么做。

复选框

<div class="form-group clearfix">
                                <label for="" class="col-sm-2 col-form-label">Arch (es) </label>
                                <div class="col-sm-10">
                                    <label class="control-label" for="inputError" style="color: red"><i
                                            id="arch_upper_error"></i></label>
                                    <div class="demo-checkbox">

                                        <input id="md_checkbox_1" name="arch_upper" value="41" class="chk-col-black"
                                               type="checkbox">
                                        <label for="md_checkbox_1">Upper</label>

                                        <input id="md_checkbox_2" name="arch_lower" value="41" class="chk-col-black"
                                               type="checkbox">
                                        <label for="md_checkbox_2">Lower</label>


                                    </div>
                                </div>
                            </div>

我在 laravel 验证中尝试过,但我知道它是错误的,因为两者都需要,但我希望至少选中一个复选框。

 public function rules()
    {
        return [
            'arch_lower' => 'required',
            'agarch_upper' => 'required',
         ,


        ];
    }

【问题讨论】:

  • 请不要标记垃圾邮件。如果与这些插件无关,请勿使用 jQuery 和 jQuery Validate 标记问题。已编辑。

标签: laravel validation


【解决方案1】:

我认为你可以使用 Laravel 的 required-without 方法:

验证中的字段必须存在且不为空,仅当有 的其他指定字段不存在。

实现看起来像这样:

'arch_upper' => 'required_without: arch_lower',

如果你有更多的复选框,你可以使用required-without-all

验证中的字段必须存在且不为空,只有当所有 的其他指定字段不存在。

实施:

'arch_upper' => 'required_without_all: arch_lower,another_checkbox',

注意:代码未经测试,如果遇到任何错误,请告诉我。

您可以在 Laravel 的官方documentantion 上阅读更多内容。

【讨论】:

    猜你喜欢
    • 2012-04-20
    • 2015-08-17
    • 1970-01-01
    • 2014-02-23
    • 2011-07-01
    • 2015-08-25
    • 2017-07-27
    • 2016-09-17
    • 1970-01-01
    相关资源
    最近更新 更多