I am trying to add custom password validation in a project which I use devise for user management. I am successful to create user, or manually change user password. However, if I exit my console and open it again, my valid user ( at last step ) becomes invalid.
I am using devise 4.6.2 and rails 5.2.0
Here is my user model
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :password,
format: { with: /\A(?=.*\d)(?=.*[A-Z])(?=.*\W)[^ ]{7,}\z/,
message: 'Password should have more than 7 characters including 1 uppercase letter, 1 number, 1 special character'
}
end
When I try in my console
u = User.new(email: 'test@test.com', password: 'Abc123!', password_confirmation: 'Abc123!')
u.valid? # TRUE
u.save
Then
u = User.last # return exact above user
u.valid? # FALSE
u.errors.full_messages # Password Password should have more than 7 characters including 1 uppercase letter, 1 number, 1 special character
Is there anything that I am doing wrong?