Class: PostalCodeFormatValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
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

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.

Parameters:

  • object (ActiveModel::Model)

    the record being validated

  • attribute (Symbol)

    the attribute name

  • value (String, nil)

    the value to validate



11
12
13
# File 'app/validators/postal_code_format_validator.rb', line 11

def validate_each(object, attribute, value)
  object.errors.add attribute, (options[:message] || "is not formatted properly") unless /((^([a-z]\d[a-z])\s?(\d[a-z]\d)$))/is.match?(value)
end