Class: NotEqualsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/not_equals_validator.rb

Overview

Checks a value not equals to another attribute’s one.

validates :password, not_equals_to: :login

Use the :allow_nil key to skip validation in case the attribute value isn’t set.

validates :password, not_equals_to: :login, allow_nil: true

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



15
16
17
18
19
20
# File 'lib/validators/not_equals_validator.rb', line 15

def validate_each(record, attribute, value)
  return if value.nil? && options[:allow_nil]
  if value == record.send(options[:to])
    record.errors.add attribute, :not_equals
  end
end