Instance Public methods
after_validation(*args, &block) Link
Defines a callback that will get called right after validation.
class Person include ActiveModel::Validations include ActiveModel::Validations::Callbacks attr_accessor :name, :status validates_presence_of :name after_validation :set_status private def set_status self.status = errors.empty? end end person = Person.new person.name = '' person.valid? # => false person.status # => false person.name = 'bob' person.valid? # => true person.status # => true
Options¶ ↑
-
:on- Specifies the contexts where this callback 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 callback 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]) -
:if- Specifies a symbol naming an instance method, or a proc or lambda, to call to determine if the callback should occur (e.g.if: :allow_validation, orif: Proc.new { |user| user.signup_step > 2 }). It should return or evaluate to atrueorfalsevalue. Several of them can be passed as an array to check multiple conditions, all of which must pass for the callback to run (e.g.if: [:allow_validation, Proc.new { |user| user.signup_step > 2 }]) -
:unless- Specifies a symbol naming an instance method, or a proc or lambda, to call to determine if the callback should not occur (e.g.unless: :skip_validation, orunless: Proc.new { |user| user.signup_step <= 2 }). It should return or evaluate to atrueorfalsevalue. Several of them can be passed as an array, and any one of them answeringtruekeeps the callback from running (e.g.unless: [:skip_validation, Proc.new { |user| user.signup_step <= 2 }])
These callbacks run in the order they are defined, so :prepend is set for them and a value passed for it is ignored.
# File activemodel/lib/active_model/validations/callbacks.rb, line 158 def after_validation(*args, &block) options = args.extract_options! options = options.dup options[:prepend] = true set_options_for_callback(options) set_callback(:validation, :after, *args, options, &block) end
before_validation(*args, &block) Link
Defines a callback that will get called right before validation.
class Person include ActiveModel::Validations include ActiveModel::Validations::Callbacks attr_accessor :name validates_length_of :name, maximum: 6 before_validation :remove_whitespaces private def remove_whitespaces name.strip! end end person = Person.new person.name = ' bob ' person.valid? # => true person.name # => "bob"
Options¶ ↑
-
:on- Specifies the contexts where this callback 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 callback 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]) -
:if- Specifies a symbol naming an instance method, or a proc or lambda, to call to determine if the callback should occur (e.g.if: :allow_validation, orif: Proc.new { |user| user.signup_step > 2 }). It should return or evaluate to atrueorfalsevalue. Several of them can be passed as an array to check multiple conditions, all of which must pass for the callback to run (e.g.if: [:allow_validation, Proc.new { |user| user.signup_step > 2 }]) -
:unless- Specifies a symbol naming an instance method, or a proc or lambda, to call to determine if the callback should not occur (e.g.unless: :skip_validation, orunless: Proc.new { |user| user.signup_step <= 2 }). It should return or evaluate to atrueorfalsevalue. Several of them can be passed as an array, and any one of them answeringtruekeeps the callback from running (e.g.unless: [:skip_validation, Proc.new { |user| user.signup_step <= 2 }]) -
:prepend- Iftrue, the callback runs before the ones already defined rather than after them.