Method: AWS::Record::Validations#validates_acceptance_of

Defined in:
lib/aws/record/validations.rb

#validates_acceptance_of(*attributes, options = {}, &block) ⇒ Object

Note:

Most validators default :allow_nil to false, this one defualts to true

Note:

This validator should not be used with multi-valued attributes

This validation method is primariliy intended for ensuring a form checkbox (like an EULA agreement or terms of service acknowledgement) is checked.

class User < AWS::Record::Base
  boolean_attr :terms_of_service
  validates_acceptance_of :terms_of_service
end

Virtual Attributes

If you choose to validate the acceptance of a non-existant attribute then a setter and a getter will be added automtically for you.

class User < AWS::Record::Base
  validates_acceptance_of :terms_of_service
end

user = User.new
user.respond_to?(:terms_of_service)  #=> true
user.respond_to?(:terms_of_service=) #=> true

Accepted Values

The default behavior for validates_acceptance_of is to add an error when the value is ‘1’ or true. Also note, this validation method defaults :allow_nil to true.

  • nil implies the field was omitted from the form and therefore should not be validated

    class User < AWS::Record::Base
      validates_acceptance_of :terms_of_service
    end
    
    u = User.new
    u.terms_of_service #=> nil
    u.valid?           #=> true
    
  • ‘1’ is the default value for most checkbox form helpers, and # therefore indicates an accepted value.

  • true is how boolean attributes typecast ‘1’. This is helpful when you have your checkbox post its value to a :boolean_attr.

Multi-Valued Attributes

This validator works only with single-valued attributes. If you need to validate that all of the values in a set are true, then use #validates_inclusion_of.

Parameters:

  • attributes

    A list of attribute names to validate.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :accpet (mixed)

    Specify an additional accepted value.

    validates_acceptance_of :agree, :accept => ‘yes’

  • :message (String)

    A custom error message. The defualt :message is “must be accepted”.

  • :allow_nil (Boolean) — default: true

    Skip validation if the attribute value is nil.

  • :on (Symbol) — default: :save

    When this validation is run. Valid values include:

    • :save:

    • :create:

    • :update:

  • :if (Symbol, String, Proc)

    Specifies a method or proc to call. The validation will only be run if the return value is of the method/proc is true (e.g. :if => :name_changed? or :if => lambda{|book| book.in_stock? }).

  • :unless (Symbol, String, Proc)

    Specifies a method or proc to call. The validation will not be run if the return value is of the method/proc is false.



191
192
193
# File 'lib/aws/record/validations.rb', line 191

def validates_acceptance_of *args
  validators << AcceptanceValidator.new(self, *args)
end