2

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

3
  • 'email' => 'alreadyAddedAsBeneficiary|required|email|unique:users,email|max:255', didn't work right? Commented Feb 10, 2017 at 11:29
  • nop i had tried it, doesn't work the way too Commented Feb 10, 2017 at 11:32
  • In this example, if the required rule on the title attribute fails, the unique rule will not be checked. Rules will be validated in the order they are assigned. Is what the site says btw. laravel.com/docs/5.4/validation Commented Feb 13, 2017 at 20:55

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

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.