Class: JpAddressComplement::AddressValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/jp_address_complement/validators/address_validator.rb

Overview

ActiveModel::Validator を継承した郵便番号・住所整合性バリデーター

Examples:

class User
  include ActiveModel::Validations
  validates_with JpAddressComplement::AddressValidator,
                 postal_code_field: :postal_code,
                 address_field: :full_address
end

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jp_address_complement/validators/address_validator.rb', line 19

def validate(record)
  postal_code = record.public_send(postal_code_field)
  address = record.public_send(address_field)

  return if (postal_code.nil? || postal_code.to_s.strip.empty?) ||
            (address.nil? || address.to_s.strip.empty?)

  return if JpAddressComplement.valid_combination?(postal_code, address)

  record.errors.add(address_field, :invalid_combination,
                    message: 'と郵便番号の組み合わせが正しくありません')
end