Class: PostalCodeValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- PostalCodeValidator
- Defined in:
- lib/validators/postal_code_validator.rb
Constant Summary collapse
- REGEXP =
keys are ISO-3366-1 alpha2
{ nl: /\A\d{4}\s*?\w{2}\z/, }.freeze
Instance Method Summary collapse
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/validators/postal_code_validator.rb', line 8 def validate_each record, attribute, value key = ([:country] || I18n.locale).downcase.to_sym key = :gb if key == :uk # Since it's easy to get this wrong raise ArgumentError, "There is no validation for the country `#{key}'" if REGEXP[key].nil? record.errors.add attribute, ([:message] || I18n.t('rails_validations.postal_code.invalid')) unless value =~ REGEXP[key] end |