Sha256: 529cef9fb23d90fadadf876ef3d27f272994a69b3ba39595c458d3a3d40e3a5c

Contents?: true

Size: 724 Bytes

Versions: 17

Compression:

Stored size: 724 Bytes

Contents

class ZipValidator < ActiveModel::EachValidator

  def initialize(options = {})
    super(options)
    @other = options[:other]
  end

  def validate_each(record, attribute, value)
    zip1 = value
    zip2 = get_other_value(record)

    return if (zip1.nil? or zip1.empty?) and (zip2.nil? or zip2.empty?)

    unless validate_zip(zip1, zip2)
      record.errors.add(attribute, I18n.t('errors.messages.zip'))
    end
  end

  private

  def validate_zip(zip1, zip2)
    return false unless zip1
    return false unless zip1.match(/^[0-9|0-9]{3}$/)

    return false unless zip2
    return false unless zip2.match(/^[0-9|0-9]{4}$/)

    true
  end

  def get_other_value(record)
    record.__send__(@other)
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
bizside-2.3.9 validations/zip_validator.rb
bizside-3.0.3 validations/zip_validator.rb
bizside-2.3.8 validations/zip_validator.rb
bizside-3.0.2 validations/zip_validator.rb
bizside-3.0.1 validations/zip_validator.rb
bizside-3.0.0 validations/zip_validator.rb
bizside-2.3.7 validations/zip_validator.rb
bizside-2.3.6 validations/zip_validator.rb
bizside-2.3.5 validations/zip_validator.rb
bizside-2.3.4 validations/zip_validator.rb
bizside-2.3.3 validations/zip_validator.rb
bizside-2.3.2 validations/zip_validator.rb
bizside-2.3.1 validations/zip_validator.rb
bizside-2.3.0 validations/zip_validator.rb
bizside-2.2.3 validations/zip_validator.rb
bizside-2.2.2 validations/zip_validator.rb
bizside-2.2.1 validations/zip_validator.rb