class ActiveModel::Validations::FormatValidator
def check_validity!
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
def validate_each(record, attribute, value)
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