Class: ActiveModel::Validations::FormatValidator

Inherits:
EachValidator show all
Defined in:
activemodel/lib/active_model/validations/format.rb

Instance Attribute Summary

Attributes inherited from EachValidator

#attributes

Attributes inherited from ActiveModel::Validator

#options

Instance Method Summary collapse

Methods inherited from EachValidator

#initialize, #validate

Methods inherited from ActiveModel::Validator

#initialize, kind, #kind, #validate

Constructor Details

This class inherits a constructor from ActiveModel::EachValidator

Instance Method Details

#check_validity!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'activemodel/lib/active_model/validations/format.rb', line 14

def check_validity!
  unless options.include?(:with) ^ options.include?(:without)  # ^ == xor, or "exclusive or"
    raise ArgumentError, "Either :with or :without must be supplied (but not both)"
  end

  if options[:with] && !options[:with].is_a?(Regexp)
    raise ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash"
  end

  if options[:without] && !options[:without].is_a?(Regexp)
    raise ArgumentError, "A regular expression must be supplied as the :without option of the configuration hash"
  end
end

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
# File 'activemodel/lib/active_model/validations/format.rb', line 6

def validate_each(record, attribute, value)
  if options[:with] && value.to_s !~ options[:with]
    record.errors.add(attribute, :invalid, options.except(:with).merge!(:value => value))
  elsif options[:without] && value.to_s =~ options[:without]
    record.errors.add(attribute, :invalid, options.except(:without).merge!(:value => value))
  end
end