class VatNumberValidator

en.wikipedia.org/wiki/VAT_identification_number ec.europa.eu/taxation_customs/vies/faqvies.do#item_7

Constants

REGEXP

keys are ISO-3366-1 alpha2

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/validators/vat_number_validator.rb, line 13
def validate_each record, attribute, value 
  key = (options[:country] || I18n.locale).downcase.to_sym
  key = :gb if key == :uk  # Since it's easy to get this wrong
  raise ArgumentError, "There is no validation for the country `#{key}'" if REGEXP[key].nil?

  value = value.to_s

  valid = true
  if REGEXP[key].respond_to? :call
    valid = false unless REGEXP[key].call value
  elsif value !=~ REGEXP[key]
    valid = false
  end

  if !valid
    record.errors.add attribute, (options[:message] || I18n.t('rails_validations.vat_number.invalid'))
  end
end