【问题标题】:Laravel 5.3 custom validation messages arrayLaravel 5.3 自定义验证消息数组
【发布时间】:2017-11-19 15:10:17
【问题描述】:

我正在使用 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>

我不确定在声明消息时错误是否在请求类中,或者如果在视图中,我如何显示该自定义消息?

【问题讨论】:

标签: php laravel laravel-5.3 laravel-blade laravel-request


【解决方案1】:

您可以显示自定义错误消息,例如

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

【讨论】:

    猜你喜欢
    • 2014-05-31
    • 2017-06-28
    • 2017-12-13
    • 2016-01-29
    • 1970-01-01
    • 2016-10-21
    • 2020-07-18
    • 2017-06-01
    • 2015-07-13
    相关资源
    最近更新 更多