问题
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.
回答1:
Found out.
Validator::extend('strip_min', function ($attribute, $value, $parameters, $validator) {
$validator->addReplacer('strip_min', function($message, $attribute, $rule, $parameters){
return str_replace([':min'], $parameters, $message);
});
return strlen(strip_tags($value)) >= $parameters[0];
});
回答2:
Use this in validation.php file
'strip_min' => 'The :attribute must be at least :strip_min characters.'
来源:https://stackoverflow.com/questions/33413748/laravel-custom-validation-message-parameter