jeudi 2 août 2018

Laravel custom validation failure with a nice message

I need to add custom validation failure, I can add a message like so

$validator->errors()->add('my_number', 'validation.not_in');

and an error message I'm getting is validation.not_in

What I need is, an an error message like this - The selected My Favourite Number is invalid.

I have my \resources\lang\en\validation.php configured to put the right message and attributes to it.

Failed solution

The one solution I can think of is to go to Laravel source code and find a file vendor\laravel\framework\src\Illuminate\Validation\Validator.php, find a function addFailure and make it public instead of protected.

then I'll be able to do things like

$validator->addFailure('my_number', 'not_in', []);

And when I do it, I'm getting my message The selected My Favourite Number is invalid. As expected!

But Taylor did not approve it.

Please give me another option.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire