Devise custom messages when validation fails

后端 未结 4 1367
说谎
说谎 2020-12-15 13:12

I\'m using devise as authentication engine in my app. Is there any way to use custom messages when devise validation fails. Devise provides me with the following message whe

相关标签:
4条回答
  • 2020-12-15 13:20

    You can customize your devise messages from config/locales/devise.en.yml but if you want to change to validation message then delete :validatable from Model. Then you can change a validation message like before. For example:

    validates_uniqueness_of    :email,     :case_sensitive => false, :allow_blank => true, :if => :email_changed?
    validates_format_of    :email,    :with  => Devise.email_regexp, :allow_blank => true, :if => :email_changed?
    
    validates_presence_of    :password, :on=>:create
    validates_confirmation_of    :password, :on=>:create
    validates_length_of    :password, :within => Devise.password_length, :allow_blank => true
    
    0 讨论(0)
  • 2020-12-15 13:34

    ActiveRecord en.yml is the answer I would suggest if you want to change the Validation Message for Devise

    Here is how the en.yml should look like

    en:
      activerecord:
        errors:
          models:
            user:
              attributes:
                email:
                  blank: "Please Specify an Email id"
                  taken: "Please use a different Email id"
                  invalid: "Please Specify a valid Email id"
                password:
                  blank: "Please Specify a Password"
                  confirmation: "Password does not match"
                password_confirmation:
                  blank: "Please Specify a Password Confirmation"
                first_name:
                  blank: "Please Specify First Name"
                last_name:
                  blank: "Please Specify Last Name"
            pdf:
              attributes:
                name:
                  blank: "Please Specify name to PDF"
                  taken: "Please use different name for PDF"
                attachment:
                  blank: "Please Upload a PDF Attachment"
            data_element:
              attributes:
                name:
                  blank: "Please give Element a desired name"
                  taken: "Already Created Element with given name"
                color:
                  blank: "Please assign a color to Element"
            template:
              attributes:
                name:
                  blank: "Please Specify a Name"
                  taken: "Please use a different name"
    

    I advice you to define this way instead of customizing devise validation module

    Because if you follow the above approach, it would be possible that you would skip a validation a place or two

    for Example I the remove the above devise validation module and then substitue your own in User Model

    then all the validation would work for but you would miss the validation in Change Password

    There by resulting your to login even though the password was never supplied and never given

    Keep a loop of that too

    Cheer

    Regards

    0 讨论(0)
  • 2020-12-15 13:34

    Please refer to the URL below.

    http://railscasts.com/episodes/210-customizing-devise?view=asciicast

    If the user is signing in, you can edit all the error messages in devise.en.yml under config/locales.

    If you're signing up, Devise provides its own validations out of the box without any customizing. If you want to customize it, you can edit the User model.

    Find devise :validatable and remove the :validatable option. After which, you should be able to use the usual rails validations. Note that this will cause you to have to do all the validations yourself.

    validates_presence_of :password, :message=>"Your custom error message!"

    Some usual validations:

      validates_confirmation_of :password
      validates_presence_of :password, :on => :create
      validates_presence_of :email
      validates_uniqueness_of :email
    
    0 讨论(0)
  • 2020-12-15 13:41

    Not a complete answer, but this sounds like it should be solvable with I18n, either with the devise-internal keys, or by overriding active record's validation error messages for your user model.

    Here's a similar question: Devise attributes for i18n?

    0 讨论(0)
提交回复
热议问题