2011-09-08 130 views
1

是否可以检查在:password字段中输入的内容是否与模型中的:current_password字段不相同?模型验证

喜欢的东西

validates :current_password, :format => { :with => :password, :message => "Current password can't be the same as the password" } 

不工作是什么写它的正确方法吗?

回答

0

首先,我希望你不要将密码保存为纯文本

其次一个自定义的验证会为你工作:

validate :password_is_not_the_same 

def password_is_not_the_same 
    errors.add(:password, 'Current password can\'t be the same as the password') if BCrypt::Password.new(password_digest) == password 
end 

编辑:

validate :password_is_not_the_same 

def password_is_not_the_same 
    errors.add(:password, 'Current password can\'t be the same as the password') if current_password == password 
end 
+0

不工作,它是如何检查:current_password? ,我只是想确保他们放入current_password字段的内容与密码字段不同,并给出错误信息,说明它们不能相同 – ahmet

+1

密码是如何存储的? (希望不是)纯文本,MD5/SHA哈希,还是使用Bcyrpt? (这个例子假设你正在使用Bcrypt。) –

+0

不知道我在使用设备 – ahmet