Abey
Abey

Reputation: 1418

Laravel : Validation messages for custom rule

I am trying to add a custom validation message using the below code,

$validator = Validator::make(
            $user,
            [
                'first_name' => 'required|min:2',
                'email'      => [
                    'required',
                    'email',
                    Rule::notIn(array_column(Customer::getEmails(), 'email'))
                ]
            ],
            ['email.required' => 'Email is required (Custom message)']);

I have added a custom message for email required validation. No issues there.

For Rule::notIn validation, currently it is returning The email is invalid. How can I add a custom message in this case?

Unable to find anything related in Laravel docs about this.

Upvotes: 3

Views: 213

Answers (1)

andcl
andcl

Reputation: 3548

Try this:

$validator = Validator::make(
        $user,
        [
            'first_name' => 'required|min:2',
            'email'      => [
                'required',
                'email',
                Rule::notIn(array_column(Customer::getEmails(), 'email'))
            ]
        ],
        [
            'email.required' => 'Email is required (Custom message)',
            'email.email' => 'Your custom message here',
            'email.not_in' => 'Your custom message here',
        ]
);

Upvotes: 2

Related Questions