1

您好,我是 ruby​​ on rails 的新手,我在验证旧密码时遇到问题。如果我要进入编辑帐户页面并且如果我保存旧密码验证显示即使编辑帐户页面和更改密码页面是不同的页面但它们在一个模型中共享。请帮助我。

这是我的模型:

validates_presence_of :firstname, :message => 'First Name is required'
  validates_format_of :firstname, :with => /\A[a-zA-Z]+([a-zA-Z]|\d)*\Z/, :message => 'First name must not contain special characters'
  validates_presence_of :lastname, :message => 'Last Name is required'
  validates_format_of :lastname, :with => /\A[a-zA-Z]+([a-zA-Z]|\d)*\Z/, :message => 'Lastname name must not contain special characters'
  validates_presence_of :email, :message => 'Email Address is required'
  validates_uniqueness_of :email, :message => 'Email address is already taken, please select a new one'
  validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Please enter a valid email address"
  validates_presence_of :username, :message => 'Username is required'
  validates_uniqueness_of :username, :message => 'Username is already taken, please select a new one'
  validates_length_of :username, :within => 3..15, :message => "Username must be at least 3 - 15 characters in length", :on => :create


  validates_presence_of :password_old, :on => :update, :message => "Old password is required"

如果您需要更多信息,请发表评论....提前致谢。

4

1 回答 1

0

所以你说它不应该在更新帐户信息时检查密码。如果用户在更改密码字段中提供了新密码,您可以编写一个条件验证来检查 old_password。

validates_presence_of :password_old, :on => :update, :unless => :current_password.nil?,:message => "Old password is required"
于 2012-07-27T08:56:46.200 回答