Madan Adhikari
Madan Adhikari

Reputation: 81

add custom validation message for custom validation in laravel 5.3 not working

I am trying to add a custom validation with custom validation message and validation is working well. Now the problem is when that validation fails i want the message for that validation to be showed as validation error but some unexpected message is shown.

public function rules()
{
    Validator::extend('alreadyAddedAsBeneficiary', function ($attribute, $value, $parameters, $validator) {


        if ($event = Event::where('event_code', $this->request->get('event_id'))->first()) {

            if ($user = User::where('email', $this->request->get('email'))->first()) {

                if ($event->beneficiary()->where('user_id', $user->id)->count() > 0)
                    return false;

                return true;

            }

        }

        AppHelper::unAuthorizedAccess(AppHelper::getErrorCode('invalid-request'));

    });

    return [
        'first_name' => 'required|max:225|min:1',
        'middle_name' => 'max:225',
        'last_name' => 'max:225',
        'email' => 'required|email|unique:users,email|max:255|alreadyAddedAsBeneficiary',
        'password' => 'required|max:60',
    ];
}

public function messages()
{
    return [
        'email.unique' => 'User already exist in system.',
        'email.alreadyAddedAsBeneficiary' => 'This user is already added as beneficiary.'
    ];
}

This message is showing. But i was expecting for "This user is already added as beneficiary."

Here is the error message generated

Upvotes: 2

Views: 541

Answers (1)

Madan Adhikari
Madan Adhikari

Reputation: 81

Found the solution, i changed the validation rule ie. alreadyAddedAsBeneficiary to already_added_as_beneficiary and now it is working.

Upvotes: 2

Related Questions