Class: ZipCodeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/models/zip_code_validator.rb

Constant Summary collapse

FORMAT =
/\A\d{5}\z/

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
# File 'app/models/zip_code_validator.rb', line 4

def validate_each(record, attribute, value)
  if value.blank?
    record.errors[attribute] << 'is required' unless options[:allow_blank] || options[:allow_nil]
    return
  end

  record.errors[attribute] << (options[:message] || 'must be a 5-digit ZIP code') unless value =~ FORMAT
end