我有这两个验证规则:
attr_accessor :validate_step
validates :full_name, :presence => {:message => 'Full Name cannot be blank.'}, :allow_blank => true, :length => {:minimum => 3, :maximum => 50}, :if => :step_two?
validates :birthdate, :presence => {:message => 'Birthdate cannot be blank.'}, :if => :step_two?
def step_two?
validate_step == 'two'
end
这是表格:
= form_for @user, :validate => true do |f|
= hidden_field_tag :validate_step, 'two'
.control-group
= f.label 'Full Name'
= f.text_field :full_name
.control-group
= f.label 'Birthdate'
= f.text_field :birthdate
这是表格的第二步。当我将这两个字段留空时,将保存所有内容,我看不到预期的验证错误。
我也尝试过这样做:
validates :full_name, :presence => {:message => 'Full Name cannot be blank.'}, :allow_blank => true, :length => {:minimum => 3, :maximum => 50}, :if => Proc.new { |user| user.validate_step == 'two' }
validates :birthdate, :presence => {:message => 'Birthdate cannot be blank.'}, :if => Proc.new { |user| user.validate_step == 'two' }
但结果是一样的,我没有看到验证错误——我做错了什么?