Class: IsACuracaoIdNumberValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/app/validators/is_a_curacao_id_number_validator.rb

Overview

usage:

in your model, add:

validates :id_number, must_be_present => true, is_a_curacao_id_number => true;

Instance Method Summary collapse

Instance Method Details

#help_messageObject



6
7
8
# File 'lib/app/validators/is_a_curacao_id_number_validator.rb', line 6

def help_message
  "Een geldig ID nummer bestaat uit:<br />4 cijfers voor het jaar<br />2 cijfers voor de maand<br />2 cijfers voor de dag<br />2 cijfers voor het volgnummer<br />Bijvoorbeeld: 1990021123."
end

#validate_each(record, attribute, value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/app/validators/is_a_curacao_id_number_validator.rb', line 10

def validate_each(record, attribute, value)
  if value =~ /^[0-9]{10}$/
    year  = value[0..3].to_i
    month = value[4..5].to_i
    day   = value[6..7].to_i
    number= value[8..9].to_i
    begin
      DateTime.civil(year, month, day)
    rescue ArgumentError
      record.errors[attribute] << (options[:message] || "is geen geldig ID nummer voor Curacao." )
    end
  else
    record.errors[attribute] << (options[:message] || "moet bestaan uit tien cijfers (bijvoorbeeld 1983040812)." )
  end
end