password confirmation error messaage on password_confirmation field

749 Views Asked by At

I have my user models validation for password confirmation like this

validate_confirmation_of :password

This add the error message doesn't match to the password field, but I need this error message on the password_confirmation field.

Can this be achieved in any other way.? I need this because, I use client side validations to show errors in form and I want this error to appear on the password_confirmation field rather than on the password field.

1

There are 1 best solutions below

0
On

You can write a simple custom validation:

class User
  validate :password_confirmation_matches_password

  def password_confirmation_matches_password
    if password != password_confirmation
      errors.add(:password_confirmation, "isn't the same")
    end
  end
end