AnApprentice
AnApprentice

Reputation: 110960

How to override a model validates errors message in the model

My app is Rails 3 + Devise. I'm building a sign up form for users. I'm working to have a helpful error message should the user not input an email address.

I believe Devise has some magic going on that's making this a mess. In my user model I have:

user.rb

validates :email,:presence => {:message => "XXXXXXX."}

My en.yml:

en:
    activerecord:
        attributes:
            user:
                fname: "First name"
                lname: "Last name"
                photo_content_type: "Picture"
        errors:
            messages:
                blank: "cannot be blank"
                too_short: "is too short (minimum %{count} characters)"
                too_long: "is too long (maximum %{count} characters)"
            models:
                user:
                    attributes:
                        email:
                            taken: "the email address %{value} has already been registered"
                        password:
                            too_short: "the password is too short (minimum %{count} characters)"

In my signup page, if the user does not enter an email, I'm getting the following @errors:

@messages={:email=>["cannot be blank", "XXXXXXX"]}>

Why am I getting two error messages? How can I get just one error message? I need to find a way to remove the "cannot be blank". Can that be overwritten in the user.rb?

Upvotes: 2

Views: 1946

Answers (1)

akhanubis
akhanubis

Reputation: 4232

You could leave the validation done by Devise, but change its message. According to the guides, you should write your custom message for the key activerecord.errors.models.user.attributes.email.blank so it would take precedence over the default:

en:
activerecord:
    attributes:
        user:
            fname: "First name"
            lname: "Last name"
            photo_content_type: "Picture"
    errors:
        messages:
            blank: "cannot be blank"
            too_short: "is too short (minimum %{count} characters)"
            too_long: "is too long (maximum %{count} characters)"
        models:
            user:
                attributes:
                    email:
                        blank: "XXXXXXX."
                        taken: "the email address %{value} has already been registered"
                    password:
                        too_short: "the password is too short (minimum %{count} characters)"

Upvotes: 7

Related Questions