2011-09-08 172 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