我正在使用 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>
我不确定在声明消息时错误是否在请求类中,或者如果在视图中,我如何显示该自定义消息?