Class: CommerceNumberValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- CommerceNumberValidator
- Defined in:
- lib/validators/commerce_number_validator.rb
Overview
Chamber of commerce number
Constant Summary collapse
- REGEXP =
keys are ISO-3366-1 alpha2
{ # KvK nummer; 8 digits. Allow space, -, and . for formatting nl: ->(v) { v.gsub(/[.\- ]/, '') =~ /\A\d{8}\z/ }, }.freeze
Instance Method Summary collapse
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/validators/commerce_number_validator.rb', line 10 def validate_each record, attribute, value key = ([: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, ([:message] || I18n.t('rails_validations.commerce_number.invalid')) end end |