Method: ActiveModel::Validations::ClassMethods#validates_each
- Defined in:
- lib/active_model/validations.rb
#validates_each(*attr_names, &block) ⇒ Object
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]) -
: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, orif: Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to atrueorfalsevalue. -
: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.
85 86 87 |
# File 'lib/active_model/validations.rb', line 85 def validates_each(*attr_names, &block) validates_with BlockValidator, _merge_attributes(attr_names), &block end |