3

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.

Abey
  • 1,408
  • 11
  • 26
  • Possible duplicate of [Custom message laravel Validation](https://stackoverflow.com/questions/45007905/custom-message-laravel-validation) – Masivuye Cokile Jan 09 '19 at 09:44
  • 1
    @MasivuyeCokile This is a whole different case. Please read it completely. – Abey Jan 09 '19 at 09:45

1 Answers1

3

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',
        ]
);
andcl
  • 3,342
  • 7
  • 33
  • 61