Laravel Custom Validation Parameter

I am using laravel 5.1. I have a summernotejs form element. I have successfully created a custom validation rule that takes HTML code from form input, breaks tags, and invokes strlen()text content. Therefore, I can see the length of the 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 invoke the validation rule by specifying: strip_min:10or strip_min:20, etc., with the number being the minimum string length after the tags are removed,

I want to add a custom message, indicating that the length of the content should be at least ncharacters.

Laravel's documentation on this aspect is useless. I opened a file validation.phpcontaining all the error messages.

I added a key strip_minto the message array with the message:The :attribute must be at least :min characters.

When I test it, I get an error message:

The notice must be at least :min characters.

How to convert :minto the number specified in the validation rule? I read the documentation , but this is everywhere, and I cannot figure out how easy it is to replace: min with the given number.

+4
source share
2 answers

Detected.

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];
});
+8
source

Use this in validation.php file

    'strip_min' => 'The :attribute must be at least :strip_min characters.'        
0
source

Source: https://habr.com/ru/post/1613646/


All Articles