`
fei1985
  • 浏览: 34486 次
  • 性别: Icon_minigender_1
  • 来自: 济南
最近访客 更多访客>>
社区版块
存档分类
最新评论

rails 3 新的 验证写法

阅读更多

 

在源文件中看到新的写法 和前面相差很多, 不过老方案在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?

 

感觉比原来的写起来越简单 一个属性的验证方案定义在一起

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics