gsk
gsk

Reputation: 2379

Custom error message is not working laravel5.1 form request?

Custom error message in form request class in not working, my form request class given below,

class FileRequest extends Request {

    protected $rules = [
        'title' => ['required', 'max:125'],
        'category_id' => ['required', 'integer', 'exists:file_categories,id']
    ];
    public function authorize() {
        return true;
    }
    public function rules() {
        return $this->rules;
    }
    public function message() {
        return [
            "category_id.required" => 'Category required',
        ];
    }
}

Here when category_id is null, showing error message category id is required instead of Category required in laravel 5.1?

Upvotes: 0

Views: 643

Answers (2)

pinkal vansia
pinkal vansia

Reputation: 10300

It is messages, not message.

Change

public function message() 

to

public function messages()

Upvotes: 1

Jerodev
Jerodev

Reputation: 33186

You do not need to create any functions to change these messages. In the file /resources/lang/en/validation.php you can add translations for the field names you are using in the attributes array.

In your case, you would do the following:

return [
    'attributes' => [
        'category_id' => 'Category'
    ],

];

Now, whenever the category_id doesn't pass the validations, the error message will display this simply as Category.

Upvotes: 1

Related Questions