1

I'm using Laravel 5.3 and i try to set a custom message for every string with max length inside the Request Class like ...

<?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.']
       ];
    }

}

But when i submit the form, an error appears, saying "ErrorException in MessageBag.php line 245: " and the view file when the errors must be displayed.

Here's the view ...

    <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>

I'm not sure if the error is in the Request Class at the moment of declaring the messages or, if is in the view, how can i display that custom message?

zt1983811
  • 1,011
  • 3
  • 14
  • 34
Vandr01y
  • 11
  • 1

1 Answers1

0

You can display custom error message like

public function messages()
{
    return [
        'max' => 'Insert some value.',
   ];
}
Ganesh
  • 3,128
  • 2
  • 17
  • 27