Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the default error message when extending the Validation class in Laravel 4

I use made use the extend function to extend and adding custom rules on the Validation Class of Laravel 4.

Validator::extend('foo', function($attribute, $value, $parameters)
{
    return $value == 'foo';
});

When I validate the rule using the newly created custom extension, it returns validation.foo if the rule fails. Is there a way to define a generic/ default message when extending the validation class in Laravel 4?

like image 990
Abishek Avatar asked Dec 26 '22 00:12

Abishek


1 Answers

The Laravel 4 docs specifically state you need to define an error message for your custom rules.

You have two options;

Option 1:

$messages = array(
    'foo' => 'The :attribute field is foo.',
);

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

Option 2:

Specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the app/lang/xx/validation.php language file:

'custom' => array(
    'foo' => array(
        'required' => 'We need to know your foo!',
    ),
),
like image 54
Laurence Avatar answered Dec 29 '22 11:12

Laurence