ActiveRecord验证和回调 2

3 验证辅助方法Active Record提供了很多预定义的验证辅助方法,在你的类定义中可以直接使用这些方法。这些方法提供了通用的验证规则。每次验证失败,一个错误信息就会添加到对象的errors集合,并且这个信息是和栏目被有效验证有关联的。每个辅助方法都接受多个属性名,所以一行代码中可以添加几个属性的验证。它们都接受:on和:message选项,:on选项从:save(默认),:create或者:update中取值。这个值是辅助方法默认的错误信息。使用:message选项能重写辅助方法的错误信息。3.1 validates_acceptance_of当表单发送时,验证checkbox是否被选中。一个典型的例子就是需要用户确认接受一些服务条款时。这个验证是非常特殊的,在数据库中并不需要有这样的记录,如果没有这样的栏目,辅助方法会建立一个虚拟属性。class Person < ActiveRecord::Basevalidates_acceptance_of :terms_of_serviceendvalidates_acceptance_of默认的错误信息是”must be accepted”。validates_acceptance_of接受:accept选项,判断将被接受的值,默认是”1″,也可以改变:class Person < ActiveRecord::Basevalidates_acceptance_of :terms_of_service, :accept => “yes”end3.2 validates_associated当你的一个模型和其它模型有关联,并且需要验证时,可以使用这个辅助方法。当尝试保存对象时,valid?将被每个关联的对象调用。class Library < ActiveRecord::Basehas_many :booksvalidates_associated :booksend这个验证将和所有的关联类型一起工作。validates_associated默认的错误信息是”is invalid”。3.3 validates_confirmation_of当你有两个文件栏目接受相同内容时,可以使用这个辅助方法。比如你想确定email地址或者密码。这个验证创建虚拟属性,名字是栏目的名字加上”_confirmation”。class Person < ActiveRecord::Basevalidates_confirmation_of :emailend在视图模板中,使用类似下面的代码:<%= text_field :person, :email %><%= text_field :person, :email_confirmation %>email_confirmation不为空时执行这个检测,需要确认还要为确定属性添加一个presence检测。class Person < ActiveRecord::Basevalidates_confirmation_of :emailvalidates_presence_of :email_confirmationend该辅助方法默认的错误信息是”doesn’t match confirmation”3.4 validates_exclusion_of这个辅助方法验证属于的属不包含给定的设置。class Account < ActiveRecord::Basevalidates_exclusion_of :subdomain, :in => %w(www), :message => “Subdomain {{value}} is reserved.”end这个辅助有个:in选项,该选项接受不被属性验证承认的值。:in选项有个名别叫:within,使用它们的结果是一样的。这个例子使用了:message选项来显示自己设定的错误信息。该辅助方法的默认错误信息是”is not included in the list”。3.5 validates_format_of该辅助方法验证属性的值是否匹配指定的正则表达式。使用:with来指定这个正则表达式。class Product < ActiveRecord::Basevalidates_format_of :legacy_code, :with => /\A[a-zA-Z]+\Z/, :message => “Only letters allowed”end该辅助方法默认的错误信息是”is invalid”.3.6 validates_inclusion_of验证属性的值是否包含给定的值.class Coffee < ActiveRecord::Basevalidates_inclusion_of :size, :in => %w(small medium large),???????????????????????? :message => “{{value}} is not a valid size”end该辅助方法有:in选项,有个别名中叫:within,使用的结果是一样的。默认的错误信息是”is not included in the list”。3.7 validates_length_of验证属性值的长度。由于辅助方法提供了多种选项,所以可以使用不同的方法指定长度的限制:class Person < ActiveRecord::Basevalidates_length_of :name, :minimum => 2validates_length_of :bio, :maximum => 500validates_length_of :password, :in => 6..20validates_length_of :registration_number, :is => 2end* :minimum – 不能比指定的长度更小。* :maximum – 不能比指定的长度更大。* :in (or :within) – 长度必须在指定的区间以内。* :is – 长度必须和指定的相同。辅助方法的默认错误信息依赖于长度验证的类型。可以使用:wrong_length, :too_long和:too_short选项和{{count}}占位符来自己指定。class Person < ActiveRecord::Basevalidates_length_of :bio, :maximum => 1000,??? :too_long => “{{count}} characters is the maximum allwed”end可以在不同的方法中使用:tokenizer选项分割值。class Essay < ActiveRecrd::Basevalidates_length_of :content,??? :minimum => 300,??? :maximum => 400,??? :tokenizer => lambda {|str| str.scan(/\w+/)},??? :too_short => “must have least {{count}} words”,??? :too_long => “must have at most {{count}} words”endvalidates_length_of有一个别名叫validates_size_of。3.8 validates_numericality_of验证属性的值是否为数字。默认情况下匹配整数和浮点数。也可以把:only_iteger设为true,来具体指定只匹配整数。如果:only_integer设为true,那么将使用:/\A[+-]?\d+\Z/使用正则表达式验证属性值。否则,就尝试使用浮点数来转换值。上述的正则表达式允许跟一个换行符。class Player < ActiveRecord::Basevalidates_numericality_of :pointsvalidates_numericality_of :games_played, :only_integer => trueend?辅助方法另外还有一些选项增加验证的限制:* :greater_than 指定的值必须比提供的值大。* :greater_than_or_equal_to 指定的值必须大于或等于提供的值。* :equal_to 指定的值必须等于提供的值。* :less_than 指定的值必须比提供的值小。* :less_than_or_equal_to 指定的值必须小于或等于提供的值。* :odd 指定的值必须是奇数* :even 指定的值必须是偶数辅助方法默认的错误信息是”is not a number”。3.9 validates_presence_of辅助方法验证属性不为空。如果值是nil或者一个空白字符串,就使用black?方法来检测。class Person < ActiveRecord::Basevalidates_presence_of :name, :login, :emailend测试联接模型的外键:class LineItem < ActiveRecord::Basebelongs_to :ordervalidates_presence_of :order_idend自此,false.blank?返回true,如果你想要验证一个布尔型的字段是否有值,就要使用validates_inclusion_of :field_name, :in => [true, false]。默认的错误信息是”can’t be empty”。3.10 validates_uniqueness_of验证属性的值的唯一性。class Account < ActiveRecord::Basevalidates_uniqueness_of :emailend?这个验证是通过数据库查询看是否有相同记录,如果有就视为无效的数据。:scope选项能让你使用指定的其他属性,用于限制的唯一性检查。class Holiday < ActiveRecord::Basevalidates_uniqueness_of :name, :scope => :year,??? :message = “should happen once per year”end使用:case_sensitive选项定义是否区分大小写。默认值是true,也就是区分大小写。class Person < ActiveRecord::Basevalidates_uniqueness_of :name, :case_sensitive => falseend默认的错误信息是”has already been taken”。3.11 validates_each没有预定义的验证函数时,可以使用block来创建。通过validates_each来测试。下面的例子中,不想让name和surname由小写字母开头:class Person < ActiveRecord::Basevalidates_each :name, :surname do |model, attr, value|??? model.erros.add(attr, “must start with upper case”) if value =~ /\A[a-z]/endend4 通用的验证选项验证辅助方法中除了:if和:unless还有一些通用的选项。4.1 :allow_nil当一个值为nil时,会跳过验证。在validates_presence_of中使用:allow_nil会允许nil,但是其它为blank的会被拒绝。class Coffee < ActiveRecord::Basevalidates_inclusion_of :size, :in => %w(small medium large),???? :message => “{{value}} is not a valid size”, :allow_nil => trueendend4.2 :allow_blank和:allow_nil类似,如果属性的值是空白允许通过验证。class Topic < ActiveRecord::Basevalidates_length_of :title, :is => 5, :allow_blank => trueendTopic.create(“title” => “”).valid? # => trueTopic.create(“title” => nil).valid? # => true4.3 :message自定义错误信息,并添加到erros集合中。当不使用这个选项时,Active Record将使用默认的值。4.4 :on:on选项指定在发生什么行为时执行验证,默认情况是对象保存的时候。可以使用:on => :create或者:on => :update来自己指定。class Person < ActiveRecord::Basevalidates_uniqueness_of :email, :on => :createvalidates_numericality_of :age, :on => :updatevalidates_presence_of :name, :on => :saveend

我们人生中最大的懒惰,就是当我们明知自己拥有作出选择的能力,

ActiveRecord验证和回调 2

相关文章:

你感兴趣的文章:

标签云: