Module: Validatable::Macros

Included in:
ClassMethods
Defined in:
lib/validatable/macros.rb

Instance Method Summary collapse

Instance Method Details

#validates_acceptance_of(*args) ⇒ Object

call-seq: validates_acceptance_of(*args)

Encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement). Example:

class Person
  include Validatable
  validates_acceptance_of :terms_of_service
  validates_acceptance_of :eula, :message => "must be abided"
end

Configuration options:

* after_validate - A block that executes following the run of a validation
* message - The message to add to the errors collection when the validation fails
* times - The number of times the validation applies
* level - The level at which the validation should occur
* if - A block that when executed must return true of the validation will not occur
* group - The group that this validation belongs to.  A validation can belong to multiple groups


118
119
120
# File 'lib/validatable/macros.rb', line 118

def validates_acceptance_of(*args)
  add_validations(args, ValidatesAcceptanceOf)
end

#validates_associated(*args) ⇒ Object

call-seq: validates_associated(*args)

Checks the validity of an associated object or objects and adds a single error if validation fails.

class Person
  include Validatable
  attr_accessor :addresses
  validates_associated :addresses
end

Configuration options:

* after_validate - A block that executes following the run of a validation
* message - The message to add to the errors collection when the validation fails
* times - The number of times the validation applies
* level - The level at which the validation should occur
* if - A block that when executed must return true of the validation will not occur
* group - The group that this validation belongs to.  A validation can belong to multiple groups


224
225
226
# File 'lib/validatable/macros.rb', line 224

def validates_associated(*args)
  add_validations(args, ValidatesAssociated)
end

#validates_confirmation_of(*args) ⇒ Object

call-seq: validates_confirmation_of(*args)

Encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Example:

Class:
  class PersonPresenter
    include Validatable
    validates_confirmation_of :user_name, :password
    validates_confirmation_of :email_address, :message => "should match confirmation"
  end

View:
  <%= password_field "person", "password" %>
  <%= password_field "person", "password_confirmation" %>

Configuration options:

* after_validate - A block that executes following the run of a validation
* case_sensitive - Whether or not to apply case-sensitivity on the comparison.  Defaults to true.
* message - The message to add to the errors collection when the validation fails
* times - The number of times the validation applies
* level - The level at which the validation should occur
* if - A block that when executed must return true of the validation will not occur
* group - The group that this validation belongs to.  A validation can belong to multiple groups


146
147
148
# File 'lib/validatable/macros.rb', line 146

def validates_confirmation_of(*args)
  add_validations(args, ValidatesConfirmationOf)
end

#validates_each(*args) ⇒ Object

call-seq: validates_each(*args)

Validates that the logic evaluates to true

class Address
  include Validatable
  validates_each :zip_code, :logic => lambda { errors.add(:zip_code, "is not valid") if ZipCodeService.allows(zip_code) }
end

The logic option is required.

Configuration options:

* after_validate - A block that executes following the run of a validation
* group - The group that this validation belongs to.  A validation can belong to multiple groups
* if - A block that when executed must return true of the validation will not occur
* level - The level at which the validation should occur
* logic - A block that executes to perform the validation
* message - The message to add to the errors collection when the validation fails
* times - The number of times the validation applies


23
24
25
# File 'lib/validatable/macros.rb', line 23

def validates_each(*args)
  add_validations(args, ValidatesEach)
end

#validates_exclusion_of(*args) ⇒ Object



197
198
199
# File 'lib/validatable/macros.rb', line 197

def validates_exclusion_of(*args)
  add_validations(args, ValidatesExclusionOf)
end

#validates_format_of(*args) ⇒ Object

call-seq: validates_format_of(*args)

Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression provided.

class Person
  include Validatable
  validates_format_of :first_name, :with => /[ A-Za-z]/
end

A regular expression must be provided or else an exception will be raised.

Configuration options:

* after_validate - A block that executes following the run of a validation
* message - The message to add to the errors collection when the validation fails
* times - The number of times the validation applies
* level - The level at which the validation should occur
* if - A block that when executed must return true of the validation will not occur
* with - The regular expression used to validate the format
* group - The group that this validation belongs to.  A validation can belong to multiple groups


48
49
50
# File 'lib/validatable/macros.rb', line 48

def validates_format_of(*args)
  add_validations(args, ValidatesFormatOf)
end

#validates_inclusion_of(*args) ⇒ Object



201
202
203
# File 'lib/validatable/macros.rb', line 201

def validates_inclusion_of(*args)
  add_validations(args, ValidatesInclusionOf)
end

#validates_length_of(*args) ⇒ Object

call-seq: validates_length_of(*args)

Validates that the specified attribute matches the length restrictions supplied.

class Person
  include Validatable
  validates_length_of :first_name, :maximum=>30
  validates_length_of :last_name, :minimum=>30
end

Configuration options:

* after_validate - A block that executes following the run of a validation
* message - The message to add to the errors collection when the validation fails
* times - The number of times the validation applies
* level - The level at which the validation should occur
* if - A block that when executed must return true of the validation will not occur
* minimum - The minimum size of the attribute
* maximum - The maximum size of the attribute
* is - The size the attribute must be
* in - A range that the size of the attribute must fall in
* group - The group that this validation belongs to.  A validation can belong to multiple groups


74
75
76
# File 'lib/validatable/macros.rb', line 74

def validates_length_of(*args)
  add_validations(args, ValidatesLengthOf)
end

#validates_numericality_of(*args) ⇒ Object

call-seq: validates_numericality_of(*args)

Validates that the specified attribute is numeric.

class Person
  include Validatable
  validates_numericality_of :age
end

Configuration options:

* after_validate - A block that executes following the run of a validation
* message - The message to add to the errors collection when the validation fails
* times - The number of times the validation applies
* level - The level at which the validation should occur
* if - A block that when executed must return true of the validation will not occur
* group - The group that this validation belongs to.  A validation can belong to multiple groups
* only_integer - Whether the attribute must be an integer (default is false)


96
97
98
# File 'lib/validatable/macros.rb', line 96

def validates_numericality_of(*args)
  add_validations(args, ValidatesNumericalityOf)
end

#validates_presence_of(*args) ⇒ Object

call-seq: validates_presence_of(*args)

Validates that the specified attributes are not nil or an empty string

class Person
  include Validatable
  validates_presence_of :first_name
end

The first_name attribute must be in the object and it cannot be nil or empty.

Configuration options:

* after_validate - A block that executes following the run of a validation
* message - The message to add to the errors collection when the validation fails
* times - The number of times the validation applies
* level - The level at which the validation should occur
* if - A block that when executed must return true of the validation will not occur
* group - The group that this validation belongs to.  A validation can belong to multiple groups


169
170
171
# File 'lib/validatable/macros.rb', line 169

def validates_presence_of(*args)
  add_validations(args, ValidatesPresenceOf)
end

#validates_true_for(*args) ⇒ Object

call-seq: validates_true_for(*args)

Validates that the logic evaluates to true

class Person
  include Validatable
  validates_true_for :first_name, :logic => lambda { first_name == 'Jamie' }
end

The logic option is required.

Configuration options:

* after_validate - A block that executes following the run of a validation
* message - The message to add to the errors collection when the validation fails
* times - The number of times the validation applies
* level - The level at which the validation should occur
* if - A block that when executed must return true of the validation will not occur
* group - The group that this validation belongs to.  A validation can belong to multiple groups
* logic - A block that executes to perform the validation


193
194
195
# File 'lib/validatable/macros.rb', line 193

def validates_true_for(*args)
  add_validations(args, ValidatesTrueFor)
end