字段校验问题

validates_presence_of     :login, :realname, :telephone, :mobile, :department_id

validates_length_of       :site, :maximum => 10, :allow_nil => true(不是必填项必须指定为true,否则spec测试跑不过)

 

%td= f.label :site
%td= f.text_field :site,:maxlength => 10(可以用于无法出现页面校验而又要限制长度的——超过10个字符将无法再输入)

 

E-Mail地址格式 :
validates_format_of     :email,
                        :with       => /^([^@"s]+)@((?:[-a-z0-9]+".)+[a-z]{2,})$/i,
                        :message    => 'email must be valid'


网址格式:
validates_uri_existence_of :url, :with =>
        /(^$)|(^(http|https)://[a-z0-9] ([-.]{1}[a-z0-9] )*.[a-z]{2,5}(([0-9]{1,5})?/.*)?$)/ix

 

validates_uniqueness_of
 检验对象是否不重复
 用法:validates_uniqueness_of attr... [ options... ]
 参数:message text 默认: “has already been taken.”
   :on :save, :create, or :update
   :scope attr 指定范围

 

validates_presence_of
 检验对象是否为空
 用法:validates_presence_of attr... [ options... ]
 参数:message text 默认:“can’t be empty.”
   :on :save, :create, or :update

 

validates_numericality_of
 检验对象是否为数值
 用法:validates_numericality_of attr... [ options... ]
 参数:message text 默认 “is not a number.”
   :on :save, :create, or :update
   :only_integer

 

validates_format_of
 用正则检验对象
 用法:validates_format_of attr..., :with => regexp [ options... ]
 参数:message text 默认为: “is invalid.”
   :on :save, :create, or :update
   :with 正则表达式

 

validates_exclusion_of
 确定被检对象不包括指定数据
 用法:validates_exclusion_of attr..., :in => enum [ options... ]
 #enum指一切可用include?()判断的范围.
 参数:allow_nil 设为true将直接跳过nil对象.
   :in (or :within) enumerable 
   :message text 默认为: “is not included in the list.”
   :on :save, :create, or :update

 

validates_inclusion_of
 确认对象包括在指定范围
 用法:validates_inclusion_of attr..., :in => enum [ options... ]
 参数:allow_nil 设为true直接跳过nil对象
   :in (or :within) enumerable An enumerable object.
   :message text 默认:“is not included in the list.”
   :on :save, :create, or :update

 

validates_each
 使用block检验一个或一个以上参数.
 用法:validates_each attr... [ options... ] { |model, attr, value | ... }
 参数:allow_nil boolean 设为true时跳过nil对象.
   :on :save, :create, or :update

 

validates_confirmation_of
 数据重校
 用法:validates_confirmation_of attr... [ options... ]
 参数:message text 默认 “doesn’t match confirmation.”
   :on :save, :create, or :update

 

validates_associated
 查验指定的object.
 用法:validates_associated name... [ options... ]
 参数:message text 默认: is “is invalid.”
   :on :save, :create, or :update

 

validates_acceptance_of
 指定checkbox应该选中. (如:(*)我同意条款)
 用法:validates_acceptance_of attr... [ options... ]
 参数:message text  默认:“must be accepted.”
   :on :save, :create, or :update


validates_size_of            校验指定属性的长度

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值