示例:
我的想法是......
class User
include Mongoid::Document
...
validates :username,
:presence => true
validates :password,
:presence => { :if => :passord? }
...
def password?
# self.controller.to_s == 'password'
end
end
首先,我的想法是错的?
任何人都有另一个好主意吗?
答案 0 :(得分:3)
您可以将虚拟属性添加到模型和条件验证中:
class User
attr_accessor :skip_password_validation
validates :password, :unless => :skip_password_validation
end
把这样的东西放到控制器中:
user.skip_password_validation = true