我有一个更改密码表格,要求用户提供他们当前的密码。使用 client_side_validations gem,是否可以使用远程验证器/javascript 验证当前密码是否正确?如果是这样,怎么做?
应用程序/视图/密码/new.html.haml
= simple_form_for @password_form, url: passwords_path, method: :post, validate: true do |f|
= f.input :original_password
= f.input :new_password
= f.input :new_password_confirmation, required: true
= f.submit 'Save'
应用程序/表单/password_form.rb
...
attr_accessor :original_password, :new_password
validate :verify_original_password
validates_presence_of :original_password, :new_password
validates_confirmation_of :new_password
validates :new_password, password: true
def verify_original_password
unless @user.authenticate(original_password)
errors.add :original_password, "is not correct"
end
end
...