instance method
validates_each
Ruby on Rails edge
Signature
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, allow_blank: true do |record, attr, value|
record.errors.add attr, "starts with z." if value.start_with?("z")
end
end
Options
-
:on- Specifies the contexts where this validation is active. Runs in all validation contexts by defaultnil. You can pass a symbol or an array of symbols. (e.g.on: :createoron: :custom_validation_contextoron: [:create, :custom_validation_context]) -
:except_on- Specifies the contexts where this validation is not active. Runs in all validation contexts by defaultnil. You can pass a symbol or an array of symbols. (e.g.except_on: :createorexcept_on: :custom_validation_contextorexcept_on: [:create, :custom_validation_context]) -
:allow_nil- Specify a method, proc, or boolean, to skip validation if attribute isnil(e.g.allow_nil: true, orallow_nil: Proc.new { |user| user.signup_step > 2 }). -
:allow_blank- Specify a method, proc, or boolean, to skip validation if attribute isblank(e.g.allow_blank: true, orallow_blank: Proc.new { |user| user.signup_step > 2 }). -
:if- Specifies a method, proc, or string to call to determine if the validation should occur (e.g.if: :allow_validation, orif: Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to atrueorfalsevalue. Multiple methods or procs can be passed as an array to check multiple conditions, all of which must pass for validation to occur (e.g.if: [:allow_validation, Proc.new { |user| user.signup_step > 2 }]) -
:unless- Specifies a method, proc, or string to call to determine if the validation should not occur (e.g.unless: :skip_validation, orunless: Proc.new { |user| user.signup_step <= 2 }). The method, proc, or string should return or evaluate to atrueorfalsevalue. Multiple methods or procs can be passed as an array to check multiple conditions, all of which must pass for validation not to occur (e.g.unless: [:skip_validation, Proc.new { |user| user.signup_step <= 2 }])
Parameters
-
attr_namesrest -
blockblock
Source
# File activemodel/lib/active_model/validations.rb, line 98
def validates_each(*attr_names, &block)
validates_with BlockValidator, _merge_attributes(attr_names), &block
end
Defined in activemodel/lib/active_model/validations.rb line 98
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveModel::Validations::ClassMethods