Class: IdentityNumberValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/kangal/identity_number.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kangal/identity_number.rb', line 5

def validate_each(record, attribute, value)

  ActiveSupport::Deprecation.warn "`identity_number: :true` is deprecated and may be removed from future releases, use `tckn: true` instead.", caller

  return if options[:allow_nil] && value.nil?
  return if options[:allow_blank] && value.blank?

  valid = false
  val = value.to_s

  if val.size == 11 && val[0].to_i != 0
    valid = check_tenth_character(val)
    valid = check_eleventh_character(val)
    valid = double_check_eleventh_character(val)
  end

  record.errors.add attribute, (options[:message] || I18n.t(:invalid, :scope => 'kangal.validations.identity_number')) unless valid
end