Class: IsraeliPostalCodeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/israeli/active_model/israeli_postal_code_validator.rb

Overview

Validates that an attribute is a valid Israeli postal code (Mikud).

Examples:

Basic usage

class Address < ApplicationRecord
  validates :postal_code, israeli_postal_code: true
end

With options

class Address < ApplicationRecord
  validates :postal_code, israeli_postal_code: { allow_blank: true }
end

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/israeli/active_model/israeli_postal_code_validator.rb', line 17

def validate_each(record, attribute, value)
  return if value.blank? && options[:allow_blank]
  return if value.nil? && options[:allow_nil]

  return if Israeli::Validators::PostalCode.valid?(value)

  reason = Israeli::Validators::PostalCode.invalid_reason(value)
  record.errors.add(
    attribute,
    options[:message] || :invalid,
    reason: reason
  )
end