rails 验证自定义方法
可以自定义validate(), 这个方法在每次保存数据时都会被调用.
如:
def validateif name.blank? && email.blank?errors.add_to_base("You mustspecify a name or an email address")endend同时也可以自定义 validate_on_create(),validate_on_update()方法.
valid?()方法可以随时调用,用来测试数据是否能通过校验
返回的错误信息可用 error_messages_for(model)方法显示.
如:<%= error_messages_for 'article' %>
校验大全:
validates_acceptance_of指定checkbox应该选中. (如:(*)我同意条款)用法:validates_acceptance_of attr... [ options...]参数:message text 默认:“mustbe accepted.”:on :save,:create, or :update实例:class Order < ActiveRecord::Basevalidates_acceptance_of:terms,:message=> "Please accept the terms to proceed"endvalidates_associated查验指定的object.用法:validates_associated name... [ options...]参数:message text 默认: is “is invalid.”:on :save,:create, or :update实例:class Order < ActiveRecord::Basehas_many :line_itemsbelongs_to :uservalidates_associated:line_items,:message=> "are messed up"validates_associated:userendvalidates_confirmation_of数据重校用法:validates_confirmation_of attr... [options... ]参数:message text 默认 “doesn’t matchconfirmation.”:on :save,:create, or :update实例:对密码表:<%= password_field "user", "password"%><br /><%= password_field "user","password_confirmation" %><br />#第二表名为xxxx_confirmationclass User < ActiveRecord::Basevalidates_confirmation_of:passwordendvalidates_each使用block检验一个或一个以上参数.用法:validates_each attr... [ options... ] {|model, attr, value| ... }参数:allow_nilboolean 设为true时跳过nil对象.:on :save,:create, or :update实例:class User < ActiveRecord::Basevalidates_each :name, :email do|model, attr, value|ifvalue =~ /groucho|harpo|chico/imodel.errors.add(attr,"Youcan't be serious, #{value}")endendendvalidates_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实例:class User < ActiveRecord::Basevalidates_exclusion_of:genre,:in=> %w{ polka twostep foxtrot },:message=>"no wild music allowed"validates_exclusion_of:age,:in=> 13..19,:message=>"cannot be a teenager"endvalidates_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实例:class User < ActiveRecord::Basevalidates_inclusion_of:gender,:in=> %w{ male female },:message=>"should be 'male' or 'female'"validates_inclusion_of:age,:in=> 0..130,:message=>"should be between 0 and 130"endvalidates_format_of用正则检验对象用法:validates_format_of attr..., :with =>regexp [ options... ]参数:message text 默认为: “is invalid.”:on :save,:create, or :update:with正则表达式实例:class User < ActiveRecord::Basevalidates_format_of :length,:with => /^\d+(in|cm)/endvalidates_length_of检查对象长度用法:validates_length_of attr..., [ options...]参数:in (or :within) range:isinteger:minimuminteger:maximuminteger:message text默认文字会根据参数变动,可使用%d取代确定的最大,最小或指定数据.:on :save,:create, or :update:too_longtext 当使用了 :maximum后的 :message:too_shorttext ( :minimum ):wrong_length( :is)实例:class User < ActiveRecord::Basevalidates_length_of :name,:maximum => 50validates_length_of :password,:in => 6..20validates_length_of :address,:minimum => 10,:message=>"seems too short"endvalidates_numericality_of检验对象是否为数值用法:validates_numericality_of attr... [options... ]参数:message text 默认 “is not a number.”:on :save,:create, or :update:only_integer实例:class User < ActiveRecord::Basevalidates_numericality_of:height_in_metersvalidates_numericality_of :age,:only_integer => trueendvalidates_presence_of检验对象是否为空用法:validates_presence_of attr... [ options...]参数:message text 默认:“can’t be empty.”:on :save,:create, or :update实例:class User < ActiveRecord::Basevalidates_presence_of :name,:addressendvalidates_uniqueness_of检验对象是否不重复用法:validates_uniqueness_of attr... [ options...]参数:message text 默认: “has already beentaken.”:on :save,:create, or :update:scope attr指定范围实例:class User < ActiveRecord::Basevalidates_uniqueness_of:nameendclass User < ActiveRecord::Basevalidates_uniqueness_of :name,:scope =>"group_id"end#指定在同一group_id的条件下不重复.常用正则: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