刺激性验证红宝石

时间:2012-07-27 07:07:03

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.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"

如果您需要更多信息,请提前评论。

1 个答案:

答案 0 :(得分:0)

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

validates_presence_of :password_old, :on => :update, :unless => :current_password.nil?,:message => "Old password is required"