Class: NilValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/nil_validator.rb

Overview

Validator to ensure an attribute is nil. Intended for use conditionally with :if or :unless to ensure an attribute is nil under one condition while a different validation, such as :presence or :inclusion is used under the dual of that condition.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ void

This method returns an undefined value.

Validates that value is nil.

Parameters:

  • record (#errors, ApplicationRecord)

    an ActiveModel or ActiveRecord

  • attribute (Symbol)

    name of attribute being validated.

  • value (#nil?)

    value of attribute to check with nil?



11
12
13
14
15
# File 'app/validators/nil_validator.rb', line 11

def validate_each(record, attribute, value)
  unless value.nil?
    record.errors.add attribute, 'must be nil'
  end
end