rails模型验证大全

来源:互联网 发布:最新网络诈骗案 编辑:程序博客网 时间:2024/05/22 17:07
  validates_presence_of  :你要验证的属性,  :messaege => '用户名不能为空!'

validates_uniqueness_of :你要验证的属性,  :message 
=> "国家代码重复"

validates_length_of :你要验证的属性, :in 
=> 3..15, :message => "用户名长度须为3到15位字母或数字"

  attr_accessor :password1
  attr_accessor :password1_confirmation
  validates_confirmation_of :password1, :message 
=> "确认密码不一致!"
  validates_presence_of :password, :message 
=> "密码不能为空!"

  validates_format_of  :你要验证的属性 ,
    :with 
=> %r{.(jpg|gif|png)$}i  ,
    :message 
=> "图片格式必须为 .jpg 或 .gif 或 .png 格式."

validates_numericality_of :orderby, :only_integer 
=> true,  :message => "顺序号必须是数值!"

validates_numericality_of :你要验证的属性,:message 
=> "必须是数字!", :only_integer => true
原创粉丝点击