Laravel Extended Validation custom message
Asked Answered
R

4

20

I wanted to create this extended validation.

Validator::extend('my_custom_validation_rule', function ($attribute, $value, $parameters) {
   // I guess I should be setting the error message for this here.(Its dynamic)
   // We can return true or false here depending upon our need.  
}

I would use this rule like this

'my_field' => 'required|my_custom_validation_rule',

I want to use some dynamic message for the error of "my_custom_validation_rule"

I was unable to find something from the documentation about it. Is there anyway to do it ?

Relentless answered 4/5, 2015 at 7:57 Comment(2)
Look again: laravel.com/docs/5.0/validation#custom-error-messagesPejsach
I want to provide the message in Validator::extend('my_custom_validation_rule', s closure itself, is it possible ?Relentless
P
42

The extend method allows to pass the message as a third argument:

Validator::extend('my_custom_validation_rule', function ($attribute, $value, $parameters) {
    // ...
}, 'my custom validation rule message');

By default you can only use dynamic variable, which is :attribute. If you want to add more use Validator::replacer():

Validator::replacer('my_custom_validation_rule', function($message, $attribute, $rule, $parameters){
    return str_replace(':foo', $parameters[0], $message);
});
Pejsach answered 4/5, 2015 at 11:49 Comment(1)
Is there any way I can make that message dynamic ? I mean my message varies depending upon the values of $attribute, $value, $parameters - Can I use closure like the second parameter to extend ?Relentless
J
9

This is basically the same way as @lukasgeiter answer, but in case you need to manage dynamic variable inside the extend function, you can use $validator->addReplacer inside the extend directly.

Validator::extend('my_custom_validation_rule', function ($attribute, $value, $parameters, $validator) {

    // Test custom message
    $customMessage = request()->get('foo') 
        ? "Foo doesn't exist"
        : "Foo exist";

    // Replace dynamic variable :custom_message with $customMessage
    $validator->addReplacer('my_custom_validation_rule', 
        function($message, $attribute, $rule, $parameters) use ($customMessage) {
            return \str_replace(':custom_message', $customMessage, $message);
        }
    );

    // Test error message. (Make it always fail the validator)
    return false;

}, 'My custom validation rule message. :custom_message');
Jagir answered 2/10, 2019 at 2:25 Comment(0)
C
5

You can also define the message for your custom validation rule under validation translations file.

/resources/lang/en/validation.php

....
'unique'                    => 'The :attribute has already been taken.',
'uploaded'                  => 'The :attribute failed to upload.',
'url'                       => 'The :attribute format is invalid.',
//place your translation here
'my_custom_validation_rule' => 'The :attribute value fails custom validation.'
Caddaric answered 6/12, 2017 at 14:54 Comment(0)
F
1

possible (not very elegant) workaround is :

$message = 'my custom validation rule message' . request()->get('param');
Validator::extend('my_custom_validation_rule', function ($attribute, $value, $parameters) {
    //
}, $message);
Fireboat answered 26/10, 2018 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.