ruby-on-railsbcrypt-ruby

Password length validation never passes with has_secure_password


I'm trying to add a length validation to password, but it always errors out with 'Password is too short'.

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)"]

In the model I have

has_secure_password
validates_length_of :password, minimum: 6

If I change the validation to

validates_length_of :password, minimum: 6, allow_blank: true

The notshort password passes, but so does short.


Solution

  • I'm using Bcrypt, too. This seems to work for me:

    has_secure_password
    validates :password, length: { minimum: 6, maximum: 20 }, on: :create