Laravel 5.1 中的自定义错误消息和 `required_without_all` 验证异常
Custom Error Messages and `required_without_all` validation anomaly in Laravel 5.1
事实如下:
- 我有一个包含很多字段的表单
- 对于其中三个字段,我只需要填写其中一个
- 我已经在我的自定义表单请求中为这三个字段设置了
required_without_all:
- 我已经为我的必填字段修改了
validation.php
中的 :attribute
- 有问题的表单字段在我的应用程序中是唯一的
问题如下:
- 当我在
emerg_contact_home_phone
字段中输入一个phone数字时,其他两个不显示错误,这是正确的。
- 当我在
emerg_contact_work_phone
字段中输入 phone 数字时,emerg_contact_mobile_phone
显示错误。
- 当我在
emerg_contact_mobile_phone
字段中输入 phone 数字时,emerg_contact_home_phone
和 emerg_contact_work_phone
都显示错误。
- 显示错误信息时,
emerg_contact_mobile_phone
不显示修改后的属性"Mobile Phone",而是显示"emerg_contact_mobile_phone"。
这是我试过的:
- 我已经对所有位置的表单名称的拼写进行了三重检查。
- 我坚信问题与
emerg_contact_mobile_phone
字段有关,因此我尝试将名称更改为其他名称(即:'mobile_phone')
这是我的代码:
form.blade.php:
<tr>
<td class="col-md-4">
{!! Form::label('emerg_contact_work_phone', '* Work Phone:', array('class' => 'control-label')) !!}
</td>
<td class="{{ $errors->has('emerg_contact_work_phone') ? 'has-error' : ''}}">
{!! Form::text('emerg_contact_work_phone', null, array('class' => 'form-control')) !!}
{!! $errors->first('emerg_contact_work_phone', '<span class="help-block">:message</span>') !!}
</td>
</tr>
<tr>
<td class="col-md-4">
{!! Form::label('emerg_contact_home_phone', '* Home Phone:', array('class' => 'control-label')) !!}
</td>
<td class="{{ $errors->has('emerg_contact_home_phone') ? 'has-error' : ''}}">
{!! Form::text('emerg_contact_home_phone', null, array('class' => 'form-control')) !!}
{!! $errors->first('emerg_contact_home_phone', '<span class="help-block">:message</span>') !!}
</td>
</tr>
<tr>
<td class="col-md-4">
{!! Form::label('emerg_contact_mobile_phone', '* Mobile Phone:', array('class' => 'control-label')) !!}
</td>
<td class="{{ $errors->has('emerg_contact_mobile_phone') ? 'has-error' : ''}}">
{!! Form::text('emerg_contact_mobile_phone', null, array('class' => 'form-control')) !!}
{!! $errors->first('emerg_contact_mobile_phone', '<span class="help-block">:message</span>') !!}
</td>
</tr>
validation.php:
'attributes' => [
'givenname' => 'First Name',
'surname' => 'Last Name',
'email' => 'Email',
'emerg_contact_relationship' => 'Relationship',
'emerg_contact_givenname' => 'First Name',
'emerg_contact_surname' => 'Last Name',
'emerg_contact_work_phone' => 'Work Phone',
'emerg_contact_home_phone' => 'Home Phone',
'emerg_contact_mobile_phone' => 'Mobile Phone',
],
CustomFormRequest.php:
public function rules()
{
return [
'givenname' => 'required',
'surname' => 'required',
'email' => 'required|email|unique:employees,email,' . $this->get('id'),
'password' => 'required_with:is_user|min:6',
'password_confirmation' => 'required_with:is_user|min:6|same:password',
'aca_number' => 'unique:employees,aca_number,' . $this->get('id'),
'license_number' => 'unique:employees,license_number,' . $this->get('id'),
'base_location' => 'required',
'emerg_contact_relationship' => 'required',
'emerg_contact_givenname' => 'required',
'emerg_contact_surname' => 'required',
'emerg_contact_home_phone' => 'required_without_all:emerg_contact_work_phone, emerg_contact_mobile_phone',
'emerg_contact_work_phone' => 'required_without_all:emerg_contact_home_phone, emerg_contact_mobile_phone',
'emerg_contact_mobile_phone' => 'required_without_all:emerg_contact_home_phone, emerg_contact_work_phone',
];
}
各列应以逗号分隔,无 space:
'emerg_contact_home_phone' => 'required_without_all:emerg_contact_work_phone,emerg_contact_mobile_phone',
事实如下:
- 我有一个包含很多字段的表单
- 对于其中三个字段,我只需要填写其中一个
- 我已经在我的自定义表单请求中为这三个字段设置了
required_without_all:
- 我已经为我的必填字段修改了
validation.php
中的:attribute
- 有问题的表单字段在我的应用程序中是唯一的
问题如下:
- 当我在
emerg_contact_home_phone
字段中输入一个phone数字时,其他两个不显示错误,这是正确的。 - 当我在
emerg_contact_work_phone
字段中输入 phone 数字时,emerg_contact_mobile_phone
显示错误。 - 当我在
emerg_contact_mobile_phone
字段中输入 phone 数字时,emerg_contact_home_phone
和emerg_contact_work_phone
都显示错误。 - 显示错误信息时,
emerg_contact_mobile_phone
不显示修改后的属性"Mobile Phone",而是显示"emerg_contact_mobile_phone"。
这是我试过的:
- 我已经对所有位置的表单名称的拼写进行了三重检查。
- 我坚信问题与
emerg_contact_mobile_phone
字段有关,因此我尝试将名称更改为其他名称(即:'mobile_phone')
这是我的代码:
form.blade.php:
<tr>
<td class="col-md-4">
{!! Form::label('emerg_contact_work_phone', '* Work Phone:', array('class' => 'control-label')) !!}
</td>
<td class="{{ $errors->has('emerg_contact_work_phone') ? 'has-error' : ''}}">
{!! Form::text('emerg_contact_work_phone', null, array('class' => 'form-control')) !!}
{!! $errors->first('emerg_contact_work_phone', '<span class="help-block">:message</span>') !!}
</td>
</tr>
<tr>
<td class="col-md-4">
{!! Form::label('emerg_contact_home_phone', '* Home Phone:', array('class' => 'control-label')) !!}
</td>
<td class="{{ $errors->has('emerg_contact_home_phone') ? 'has-error' : ''}}">
{!! Form::text('emerg_contact_home_phone', null, array('class' => 'form-control')) !!}
{!! $errors->first('emerg_contact_home_phone', '<span class="help-block">:message</span>') !!}
</td>
</tr>
<tr>
<td class="col-md-4">
{!! Form::label('emerg_contact_mobile_phone', '* Mobile Phone:', array('class' => 'control-label')) !!}
</td>
<td class="{{ $errors->has('emerg_contact_mobile_phone') ? 'has-error' : ''}}">
{!! Form::text('emerg_contact_mobile_phone', null, array('class' => 'form-control')) !!}
{!! $errors->first('emerg_contact_mobile_phone', '<span class="help-block">:message</span>') !!}
</td>
</tr>
validation.php:
'attributes' => [
'givenname' => 'First Name',
'surname' => 'Last Name',
'email' => 'Email',
'emerg_contact_relationship' => 'Relationship',
'emerg_contact_givenname' => 'First Name',
'emerg_contact_surname' => 'Last Name',
'emerg_contact_work_phone' => 'Work Phone',
'emerg_contact_home_phone' => 'Home Phone',
'emerg_contact_mobile_phone' => 'Mobile Phone',
],
CustomFormRequest.php:
public function rules()
{
return [
'givenname' => 'required',
'surname' => 'required',
'email' => 'required|email|unique:employees,email,' . $this->get('id'),
'password' => 'required_with:is_user|min:6',
'password_confirmation' => 'required_with:is_user|min:6|same:password',
'aca_number' => 'unique:employees,aca_number,' . $this->get('id'),
'license_number' => 'unique:employees,license_number,' . $this->get('id'),
'base_location' => 'required',
'emerg_contact_relationship' => 'required',
'emerg_contact_givenname' => 'required',
'emerg_contact_surname' => 'required',
'emerg_contact_home_phone' => 'required_without_all:emerg_contact_work_phone, emerg_contact_mobile_phone',
'emerg_contact_work_phone' => 'required_without_all:emerg_contact_home_phone, emerg_contact_mobile_phone',
'emerg_contact_mobile_phone' => 'required_without_all:emerg_contact_home_phone, emerg_contact_work_phone',
];
}
各列应以逗号分隔,无 space:
'emerg_contact_home_phone' => 'required_without_all:emerg_contact_work_phone,emerg_contact_mobile_phone',