| 32 | | DEFAULT_VALIDATION_OPTIONS = { |
|---|
| 33 | | :on => :save, |
|---|
| 34 | | :allow_nil => false, |
|---|
| 35 | | :allow_blank => false, |
|---|
| 36 | | :message => nil |
|---|
| 37 | | }.freeze |
|---|
| | 27 | DEFAULT_VALIDATION_OPTIONS = { :on => :save, :allow_nil => false, :allow_blank => false, :message => nil }.freeze |
|---|
| | 28 | |
|---|
| | 29 | # Adds a validation method or block to the class. This is useful when |
|---|
| | 30 | # overriding the #validate instance method becomes too unwieldly and |
|---|
| | 31 | # you're looking for more descriptive declaration of your validations. |
|---|
| | 32 | # |
|---|
| | 33 | # This can be done with a symbol pointing to a method: |
|---|
| | 34 | # |
|---|
| | 35 | # class Comment < ActiveRecord::Base |
|---|
| | 36 | # validate :must_be_friends |
|---|
| | 37 | # |
|---|
| | 38 | # def must_be_friends |
|---|
| | 39 | # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee) |
|---|
| | 40 | # end |
|---|
| | 41 | # end |
|---|
| | 42 | # |
|---|
| | 43 | # Or with a block which is passed the current record to be validated: |
|---|
| | 44 | # |
|---|
| | 45 | # class Comment < ActiveRecord::Base |
|---|
| | 46 | # validate do |comment| |
|---|
| | 47 | # comment.must_be_friends |
|---|
| | 48 | # end |
|---|
| | 49 | # |
|---|
| | 50 | # def must_be_friends |
|---|
| | 51 | # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee) |
|---|
| | 52 | # end |
|---|
| | 53 | # end |
|---|
| | 54 | # |
|---|
| | 55 | # This usage applies to #validate_on_create and #validate_on_update as well. |
|---|
| | 56 | |
|---|
| | 57 | # Validates each attribute against a block. |
|---|
| | 58 | # |
|---|
| | 59 | # class Person < ActiveRecord::Base |
|---|
| | 60 | # validates_each :first_name, :last_name do |record, attr, value| |
|---|
| | 61 | # record.errors.add attr, 'starts with z.' if value[0] == ?z |
|---|
| | 62 | # end |
|---|
| | 63 | # end |
|---|
| | 64 | # |
|---|
| | 65 | # Options: |
|---|
| | 66 | # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update) |
|---|
| | 67 | # * <tt>allow_nil</tt> - Skip validation if attribute is nil. |
|---|
| | 68 | # * <tt>allow_blank</tt> - Skip validation if attribute is blank. |
|---|
| | 69 | # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should |
|---|
| | 70 | # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The |
|---|
| | 71 | # method, proc or string should return or evaluate to a true or false value. |
|---|
| | 72 | # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should |
|---|
| | 73 | # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The |
|---|
| | 74 | # method, proc or string should return or evaluate to a true or false value. |
|---|
| | 75 | def validates_each(*attrs) |
|---|
| | 76 | options = attrs.extract_options!.symbolize_keys |
|---|
| | 77 | attrs = attrs.flatten |
|---|
| | 78 | |
|---|
| | 79 | # Declare the validation. |
|---|
| | 80 | send(validation_method(options[:on] || :save), options) do |record| |
|---|
| | 81 | attrs.each do |attr| |
|---|
| | 82 | value = record.send(attr) |
|---|
| | 83 | next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) |
|---|
| | 84 | yield record, attr, value |
|---|
| | 85 | end |
|---|
| | 86 | end |
|---|
| | 87 | end |
|---|