Method: AWS::Record::Validations#validates_acceptance_of
- Defined in:
- lib/aws/record/validations.rb
#validates_acceptance_of(*attributes, options = {}, &block) ⇒ Object
Most validators default :allow_nil to false, this one defualts to true
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.
-
nilimplies the field was omitted from the form and therefore should not be validatedclass 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.
-
trueis 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.
191 192 193 |
# File 'lib/aws/record/validations.rb', line 191 def validates_acceptance_of *args validators << AcceptanceValidator.new(self, *args) end |