簡體   English   中英

Laravel 5.1中的自定義錯誤消息和`required_without_all`驗證異常

[英]Custom Error Messages and `required_without_all` validation anomaly in Laravel 5.1

這是事實:

  • 我有很多領域的表格
  • 對於這三個字段,我只需要填寫其中一個即可
  • 我在我的自定義表單請求中為這三個字段設置了required_without_all:
  • 我已經修改了:attributevalidation.php我必填字段
  • 有問題的表單字段在我的應用程序中是唯一的

這里是問題:

  • 當我在emerg_contact_home_phone字段中輸入電話號碼時,其他兩個不會顯示錯誤,這是正確的。
  • 當我在emerg_contact_work_phone字段中輸入電話號碼時, emerg_contact_mobile_phone顯示錯誤。
  • 當我在emerg_contact_mobile_phone字段中輸入電話號碼時, emerg_contact_home_phoneemerg_contact_work_phone顯示錯誤。
  • 當顯示錯誤消息時, emerg_contact_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',
    ];
}

列應以逗號分隔,不能有空格:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM