2017-06-16 73 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.', 
    ]; 
}