codearts
codearts

Reputation: 2956

Laravel 5.2 - Create custom validation message when extending Validator

In my AppServideProvider's boot method I've set a custom validation rule for cyrillic. Here is what it lookes like:

public function boot()
{
    Validator::extend('cyrillic', function ($attribute, $value, $parameters, $validator) {
        return preg_match('/[А-Яа-яЁё]/u', $value);
    });
}

It works as expected. However if the validation doesn't pass because of latin input I get the error message 'validation.cyrillic'. How can I fix that? How can I pass a custom message to my 'cyrillic' validation rule?

Upvotes: 1

Views: 228

Answers (1)

Burak Ozdemir
Burak Ozdemir

Reputation: 5332

If you want to define it globally, you need to edit the validation file or files, which is/are located in resources/lang/LANG/validation.php where LANG is whatever language you want to define is.

For instance, for the English ones, open the file resources/lang/en/validation.php and add your message like below.

return [
    'accepted'             => 'The :attribute must be accepted.',
    'active_url'           => 'The :attribute is not a valid URL.',
    // Add yours to somewhere in the first level of the array
    'cyrillic'             => 'The :attribute is not Cyrillic.'
]

For locally, you can define it within a Request.

public function messages()
{
    return [
        'cyrillic' => 'The :attribute is not Cyrillic.'
    ];
}

Upvotes: 1

Related Questions