0

In updating profile i use a validator class method:

class UpdateRequest extends Request {
    public function authorize() { return true; }
    public function rules() {
        return [
            'name' => 'required',
            'email' => 'required|email',
        ];
    }
}

How to add an additional validation error like:

public function postUpdate(UpdateRequest $request)
    if($user->email == $request->get('email')) {
        $request->addEerror("The email has already been taken."); //shows an fatal error        
    }
}

?

Thank you

1
  • confirm me what you want custom validation or custom validation error message? Commented Oct 8, 2015 at 12:01

1 Answer 1

1

You have not mentioned which Laravel version you are using, assuming 5.1

You can create a message array for different validation type, like the example below:

$rules = [
     'first_name' => 'required',
     'last_name' => 'required',
     'email' => 'required|email|unique:users,email,'.$user->id
];

In your resources/lang/en/validation.php file

$custom_messages = [
     'required' => 'The :attribute field is required.',
     'email' => [
         'required' => 'The email id field is required.',
         'email' => 'Please enter a valid email format.',
         'unique' => 'The email id has already been taken.',
     ]
];

This should do the trick.

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.