如何在Rails 3.2中使用has_secure_password时禁用密码/确认验证?

wgp*_*ubs 16 validation activerecord ruby-on-rails ruby-on-rails-3.2

我有一个用户模型设置如下:

class User < ActiveRecord::Base
  has_secure_password

  # callbacks -------------------------------------------------------------------------
  before_create { generate_token(:auth_token) }

  # setup accessible (or protected) attributes for your model and validation ----------
  attr_accessible :email, :password, :password_confirmation

  # validations
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email    , presence: true,
                        uniqueness: { case_sensitive: false },
                        format: { with: VALID_EMAIL_REGEX }

  validates :password             , length: { minimum: 6 } 

  validates :password_confirmation, presence: true
Run Code Online (Sandbox Code Playgroud)

但我不希望在更新用户时运行密码和/或password_confirmation验证,除非用户尝试更改其密码.

因此,如果用户在没有指定密码和/或确认的情况下更新表单中的信息,则应该成功.如果用户更新其信息并包含密码和/或确认,则应运行验证.

完成此任务的最佳方法是什么?

Zab*_*bba 21

您可以:if为两个验证器添加条件:

选项1:

validates :password             , length: { minimum: 6 }, :if => :validate_password?
validates :password_confirmation, presence: true        , :if => :validate_password?

def validate_password?
  password.present? || password_confirmation.present?
end
Run Code Online (Sandbox Code Playgroud)

选项2:

或者使用方法进行验证,将验证检查移到方法中而不是单独的validates调用:

validate :check_password, :on => :update

def check_password
  return unless password.present? || password_confirmation.present?
  ..validations for both attributes here.. 
  ..check presence, length etc. as required and add to the errors variable as necessary..
end
Run Code Online (Sandbox Code Playgroud)