2017-06-16 95 views
1

我使用Laravel 5.3和我試圖設置自定義的消息與請求類像裏面最大長度每個字符串...Laravel 5.3自定義驗證消息陣列

<?php 

namespace App\Http\Requests; 
use Illuminate\Foundation\Http\FormRequest; 

class UpdateRecordRequest extends FormRequest 
{ 
    public function authorize() 
    { 
     return true; 
    } 

    public function rules() 
    { 
     $rules = [ 
      'field_1' => 'string|max:100', 
      'field_2' => 'string|max:100', 
      'field_3' => 'string|max:100', 
      ]; 

     return $rules; 
    } 

    public function messages() 
    { 
     return [ 
      '*.max' => ['string' => 'Insert some value.'] 
     ]; 
    } 

}

但當我提交表單時,會出現一個錯誤,在MessageBag.php第245行中顯示「ErrorException:」和必須顯示錯誤的視圖文件。

這裏的景色......

<div class="form-group {{ $errors->has('field_1') ? 'has-error' : '' }}"> 
     <label for="">Field 1</label> 
     {{ Form::text('field_1', $record->field_1, ['class' => 'form-control']) }} 
     <span class="help-block">{{ $errors->first('field_1') }}</span> 
    </div> 

    <div class="form-group {{ $errors->has('field_2') ? 'has-error' : '' }}"> 
     <label for="">Field 2</label> 
     {{ Form::text('field_2', $record->field_2, ['class' => 'form-control']) }} 
     <span class="help-block">{{ $errors->first('field_2') }}</span> 
    </div> 

    <div class="form-group {{ $errors->has('field_3') ? 'has-error' : '' }}"> 
     <label for="">Field 3</label> 
     {{ Form::text('field_3', $record->field_1, ['class' => 'form-control']) }} 
     <span class="help-block">{{ $errors->first('field_3') }}</span> 
    </div> 

我不知道如果錯誤是在請求類在宣佈消息的時刻,或者,如果是在視圖中,我怎麼能顯示自定義消息?

+0

https://laravel.io/forum/05-13-2014-lost-with-messagebag-and-validation-errors自定義錯誤消息 –

回答

0

可以顯示像

public function messages() 
{ 
    return [ 
     'max' => 'Insert some value.', 
    ]; 
}