Home>

Under development of personal application
When you press the registration button without entering anything on the new registration screen

config>locales>devise.views.ja.yaml
 password: password:
  blank: "is not entered."
  invalid: "Please set including both letters and numbers."


These two will be displayed
Since it is blank, I understand that both blank and invalid are displayed, but it is easier to understand from the user's point of view if one of the error messages is displayed.
I want to display only invalid
[Beginner] How to prevent duplicate error messages in Rails validates presence
Look at this article

with_options presence: true do | i |
    i.validates: nickname
    i.validates: email
    i.validates: encrypted_password
  end
  validates: nickname, length: {maximum: 7}
  PASSWORD_REGEX =/\ A (? =. *? [A-z]) (? =. *? [\ D]) [a-z \ d] + \ z/i.freeze
  validates_format_of: password, with: PASSWORD_REGEX
  validates: password, format: {with: PASSWORD_REGEX}, allow_blank: true # ← allow_blank: I intended to prevent blank errors from appearing with true
  validates: password, presence: true, on :: create


I displayed it like this, but both are displayed
Thanks for your advice! !!

  • Answer # 1

    PASSWORD_REGEX =/\ A (? =. *? [A-z]) (? =. *? [\ D]) [a-z \ d] + \ z/i.freeze
      #valids_format_of: password, with: PASSWORD_REGEX
      validates: password, format: {with: PASSWORD_REGEX}, allow_blank: true
      validates: password, presence: true, on :: create

    I wrote it like this

    validates_format_of: password, with: PASSWORD_REGEX


    This seems to have been bad
    Find out what validates_format_pf is

  • Answer # 2

    I have two things to do.

    Disable Devise validation.,: validatableDelete

    Write your own invalidation in the User model
    validates: password presenceWhen
    validates: password form: {with:/regular expression /}, if: Proc.new {| a | a.password.resent?}

    The second if: may be smarter to write, but it works for the time being.