0

Following: https://laravel.com/docs/5.7/validation#using-closures

I have added below code in myServiceProvider

public function boot()
{
    \Validator::extend('foo', function ($attribute, $value, $parameters, $validator) {
        if('fname' === $value)
        return true;
        return false;            
    });       
}

Now, for custom error message what does this line mean? using an inline custom message array

what should be the syntax?

Defining The Error Message

You will also need to define an error message for your custom rule. You can do so either using an inline custom message array or by adding an entry in the validation language file. This message should be placed in the first level of the array, not within the custom array, which is only for attribute-specific error messages:

1 Answer 1

2

If you want to define it globally, you need to edit the validation file lang-path/validation.php.

return [
    'foo'             => 'The :attribute must be foo.'
]

For locally, you can define it within a Request.

public function messages()
{
    return [
        'foo' => 'The :attribute is not foo.'
    ];
}
Sign up to request clarification or add additional context in comments.

2 Comments

can you tell me why the $parameters are there when it comes empty always?
$parameters you need to pass, by default it'll be empty, assume foo is the validation so you can pass foo:10 so $parameters will come as an array with 10 as first element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.