instance method
validates_each
Ruby on Rails 3.0.20
Since v2.2.3Signature
validates_each(*attr_names, &block)
Validates each attribute against a block.
class Person include ActiveModel::Validations attr_accessor :first_name, :last_name validates_each :first_name, :last_name do |record, attr, value| record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z end end
Options:
-
:on- Specifies when this validation is active (default is:save, other options:create,:update). -
:allow_nil- Skip validation if attribute isnil. -
:allow_blank- Skip validation if attribute is blank. -
:if- Specifies a method, proc or string to call to determine if the validation should occur (e.g.:if => :allow_validation, or:if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value. -
:unless- Specifies a method, proc or string to call to determine if the validation should not occur (e.g.:unless => :skip_validation, or:unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.
Parameters
-
attr_namesrest -
blockblock
Source
# File activemodel/lib/active_model/validations.rb, line 86
def validates_each(*attr_names, &block)
options = attr_names.extract_options!.symbolize_keys
validates_with BlockValidator, options.merge(:attributes => attr_names.flatten), &block
end
Defined in activemodel/lib/active_model/validations.rb line 86
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveModel::Validations::ClassMethods