Rails正在跳过:create上的格式验证。在创造时,它接受任何东西。然后在更新:presence和:format都按预期工作。我怎么能改变它所以它会:make_blank on create并检查格式呢?
validates :mail, :allow_blank => true, :on => :create,
:format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)*[a-z]{2,})$/i },
:length => { :maximum => 60 },
:presence => true, :on => :update
答案 0 :(得分:1)
尝试对:create,:update和所有生命周期事件使用单独的validates
语句。当你将它们全部混合在一起时,你的:on => :update
选项基本上会覆盖:on => :create
选项。
validates :mail, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)*[a-z]{2,})$/i },
:length => { :maximum => 60 }
validates :mail, :allow_blank => true, :on => :create,
validates :mail, :presence => true, :on => :update