Class: PostalCodeFormatValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- PostalCodeFormatValidator
- Defined in:
- app/validators/postal_code_format_validator.rb
Overview
Validates that a value is formatted as a Canadian postal code (e.g. "A1A 1A1").
Instance Method Summary collapse
-
#validate_each(object, attribute, value) ⇒ void
Validates a single attribute value against the postal code pattern.
Instance Method Details
#validate_each(object, attribute, value) ⇒ void
This method returns an undefined value.
Validates a single attribute value against the postal code pattern.
11 12 13 |
# File 'app/validators/postal_code_format_validator.rb', line 11 def validate_each(object, attribute, value) object.errors.add attribute, ([:message] || "is not formatted properly") unless /((^([a-z]\d[a-z])\s?(\d[a-z]\d)$))/is.match?(value) end |