clear_validators!

clear_validators!()
Instance Public methods

Clears all of the validators and validations.

Note that this will clear anything that is being used to validate the model for both the validates_with and validate methods. It clears the validators that are created with an invocation of validates_with and the callbacks that are set by an invocation of validate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Person
  include ActiveModel::Validations
 
  validates_with MyValidator
  validates_with OtherValidator, on: :create
  validates_with StrictValidator, strict: true
  validate :cannot_be_robot
 
  def cannot_be_robot
    errors.add(:base, 'A person cannot be a robot') if person_is_robot
  end
end
 
Person.validators
# => [
#      #<MyValidator:0x007fbff403e808 @options={}>,
#      #<OtherValidator:0x007fbff403d930 @options={on: :create}>,
#      #<StrictValidator:0x007fbff3204a30 @options={strict:true}>
#    ]

If one runs Person.clear_validators! and then checks to see what validators this class has, you would obtain:

1
Person.validators # => []

Also, the callback set by validate :cannot_be_robot will be erased so that:

1
Person._validate_callbacks.empty?  # => true
doc_ruby_on_rails
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.