Andrew Fox Andrew Fox - 4 months ago 15
PHP Question

Custom Error Messages and `required_without_all` validation anomaly in Laravel 5.1

Here are the facts:


  • I have a form with many fields

  • For three of the fields, I only require one of them to be filled

  • I have set
    required_without_all:
    in my custom form request for these three fields

  • I have modified the
    :attribute
    in
    validation.php
    for my required fields

  • The form fields in question are unique within my application



Here are the issues:


  • When I enter a phone number in the
    emerg_contact_home_phone
    field, the other two do not display the error, which is correct.

  • When I enter a phone number in the
    emerg_contact_work_phone
    field, the
    emerg_contact_mobile_phone
    displays an error.

  • When I enter a phone number in the
    emerg_contact_mobile_phone
    field, both the
    emerg_contact_home_phone
    AND
    emerg_contact_work_phone
    display an error.

  • When the error messages display,
    emerg_contact_mobile_phone
    doesn't display the modified attribute "Mobile Phone", it displays "emerg_contact_mobile_phone" instead.



Here is what I've tried:


  • I have triple checked the spelling of the form names in all locations.

  • I firmly believe the issue has to do with the
    emerg_contact_mobile_phone
    field, so I tried changing the name to something different (ie: 'mobile_phone')



Here is my code:

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',
];
}

Answer

The columns should be comma separated without a space:

'emerg_contact_home_phone' => 'required_without_all:emerg_contact_work_phone,emerg_contact_mobile_phone',