在源文件中看到新的写法 和前面相差很多, 不过老方案在rails3也不会出错
Examples of using the default rails validators:
validates :terms, :acceptance => true
validates :password, :confirmation => true
validates :username, :exclusion => { :in => %w(admin superuser) }
validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create }
validates :age, :inclusion => { :in => 0..9 }
validates :first_name, :length => { :maximum => 30 }
validates :age, :numericality => true
validates :username, :presence => true
validates :username, :uniqueness => true
validates :title, :presence => true, :uniqueness => true, :length => { :maximum => 100 }
validates :budget, :presence => true, :length => { :within => 1..10000000 }
# Finally, the options :if, :unless, :on, :allow_blank and :allow_nil can be given
# to one specific validator:
#
validates :password, :presence => true, :confirmation => true, :if => :password_required?
感觉比原来的写起来越简单 一个属性的验证方案定义在一起
Rails验证
本文展示了Rails中默认验证器的使用示例,包括接受性、确认性、排除性等验证,并介绍了如何结合条件使用验证。
593

被折叠的 条评论
为什么被折叠?



