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 校验指定属性的长度