代码之家  ›  专栏  ›  技术社区  ›  FeifanZ

密码长度验证永远不会通过has_secure_Password

  •  3
  • FeifanZ  · 技术社区  · 12 年前

    我正在尝试将长度验证添加到 password ,但它总是错误地显示为“密码太短”。

    a = Artist.new(artist_name: 'Dylan', email: 'dylan@example.com', password: 'notshort')
    p a.errors.full_messages  # ["Password is too short (minimum is 6 characters)"]
    a = Artist.new(artist_name: 'Dylan', email: 'dylan@example.com', password: 'short')
    p a.errors.full_messages  # ["Password is too short (minimum is 6 characters)"]
    

    在我的模型中

    has_secure_password
    validates_length_of :password, minimum: 6
    

    如果我将验证更改为

    validates_length_of :password, minimum: 6, allow_blank: true
    

    这个 notshort 密码通过,但也通过 short .

    2 回复  |  直到 12 年前
        1
  •  4
  •   Cruz Nunez    10 年前

    我也在使用Bcrypt。这似乎对我有用:

    has_secure_password
    validates :password, length: { minimum: 6, maximum: 20 }, on: :create
    
        2
  •  0
  •   Rob Wise    12 年前

    而不是 validates_length_of ,尝试使用 validates 这样地:

    validates :password, length: { minimum: 6 }

    推荐文章