jeudi 29 octobre 2015

Laravel custom validation message parameter

I'm using laravel 5.1. I have a summernotejs form element. I've created a custom validation rule successfully, which takes the HTML provided from the form input, strips the tags, and does a strlen() call on the text content. Therefore I can see the length of a message without any tags in it.

This is my validation rule:

Validator::extend('strip_min', function ($attribute, $value, $parameters, $validator) {
    return strlen(strip_tags($value)) >= $parameters[0];
});

I call the validation rule by specifying: strip_min:10 or strip_min:20 etc, with the number being the minimum string length after stripping tags.

I want to add a custom message, saying that the content lengths needs to be a minimum of n characters long.

The Laravel documentation on this aspect is useless. I've opened my validation.php file, which contains all the error messages.

I've added the key strip_min to the message array, with the message: The :attribute must be at least :min characters.

When I test it, I get the error message:

The notice must be at least :min characters.

How can I convert :min into the number specified in the validation rule?! I've read the documentation but it's all over the place, and I cant understand how to just simply replace :min with the given number.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire