首页 > 解决方案 > Laravel 5.7 验证 required_if null

问题描述

我使用 Laravel 5.7。

这是一个联系表格,我有不同的输入字段,必须提供phone或/和地址。email如果不是这种情况,则应显示错误消息。

这是来自电话和电子邮件的 html 代码:

                 <div class="form-group col-xs-12 col-sm-6">
                    <label for="email">E-Mail</label>
                    @honeypot
                        <input type="email" name="email" id="email" value="{{ old('email') }}" class="form-control">
                        @if ( $errors->has('email') )
                            <div class="error-red">{{ $errors->first('email') }}</div>
                        @endif
                </div>

               <div class="form-group col-xs-12 col-sm-6">
                    <label for="phone">Mobile</label>
                        <input type="text" name="phone" id="phone" value="{{ old('phone') }}" class="form-control">
                        @if ( $errors->has('phone') )
                            <div class="error-red">{{ $errors->first('phone') }}</div>
                        @endif
               </div>

我首先尝试了这个,但没有错误消息:

        'firstName' => 'nullable',
        'lastName' => 'nullable',
        'email' => ['nullable', 'required_if:phone,=,null', 'email'],
        'phone' => ['nullable', 'required_if:email,=,null'],

接下来我尝试了这个(没有相同=)但它也没有工作:

        'firstName' => 'nullable',
        'lastName' => 'nullable',
        'email' => ['nullable', 'required_if:phone,null', 'email'],
        'phone' => ['nullable', 'required_if:email,null'],

有没有人看到一个错误?我已经这样做了很多次,到目前为止它一直有效。

标签: phplaravel

解决方案


推荐阅读