Syamsoul Azrien
Syamsoul Azrien

Reputation: 2742

Laravel - Customize error message that using Rule class

Based on the Laravel's documentation, the way to edit error message is like this:

$messages = [
    'email.required' => 'We need to know your e-mail address!',
];

$validator = Validator::make($input, $rules, $messages);

But what if the rule is using Rule class?

For example:

$rules = [
    'img_type'      => ['required', Rule::in(['png', 'jpeg', 'gif'])],
];

$messages = [
    'img_type.{what-to-type-here-for-Rule::in}' => 'Invalid image type',
];

$validator = Validator::make($input, $rules, $messages);

As the example above, img_type.{what-to-type-here-for-Rule::in}, I don't know how to specify the custom error message for Rule::in...

Upvotes: 0

Views: 248

Answers (1)

Jerodev
Jerodev

Reputation: 33186

The rule is just called in. So this is what you have to use.

$messages = [
    'img_type.in' => 'Invalid image type',
];

Exactly as it is defined in the default translations.

Upvotes: 4

Related Questions