Sha256: 7cafa9be328cdf333173be4871ce8af82605af37e750053629c261b3b1593961

Contents?: true

Size: 512 Bytes

Versions: 2

Compression:

Stored size: 512 Bytes

Contents

# Custom Validations for Zip Code format

# e.g. validates :zip, :zipcode_format => true
# e.g. validates :zip, :zipcode_format => { :allow_blank => true, :if => :method_returns_true }
class ZipcodeFormatValidator < ActiveModel::EachValidator
  def validate_each record, attribute, value
    format = /^\A[\d]{5}(?:[-|\s][\d]{4})?\Z$/
    message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.'
    record.errors[attribute] << (options[:message] || message ) unless value =~ format
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
format_validators-0.0.6 app/validators/zipcode_format_validator.rb
format_validators-0.0.5 app/validators/zipcode_format_validator.rb