Class: ActiveModel::Validations::ValvatValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/valvat/active_model.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) validate_each(record, attribute, value)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/valvat/active_model.rb', line 9

def validate_each(record, attribute, value)
  is_valid = Valvat::Syntax.validate(value)
  
  if is_valid && options[:lookup]
    is_valid = Valvat::Lookup.validate(value)
    is_valid.nil? && is_valid = (options[:lookup] != :fail_if_down)
  end
  
  unless is_valid
    record.errors.add(attribute, :invalid_vat, 
      :message => options[:message], 
      :country_adjective => I18n.t(
        :valvat.country_adjectives.#{(Valvat::Utils.split(value)[0] || "eu").downcase}", 
        :default => [:valvat.country_adjectives.eu", "european"]
      )
    )
  end
end