Class: ActiveModel::Validations::CpfOrCnpjValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::CpfOrCnpjValidator
show all
- Includes:
- ValidatesCpfCnpj
- Defined in:
- lib/validates_cpf_cnpj.rb
Constant Summary
ValidatesCpfCnpj::VERSION
Instance Method Summary
collapse
Instance Method Details
#validate_each(record, attr_name, value) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/validates_cpf_cnpj.rb', line 10
def validate_each(record, attr_name, value)
return if (options[:allow_nil] and value.nil?) or (options[:allow_blank] and value.blank?)
return if (options[:if] == false) or (options[:unless] == true)
return if (options[:on].to_s == 'create' and not record.new_record?) or (options[:on].to_s == 'update' and record.new_record?)
if value.to_s.gsub(/[^0-9]/, '').length <= 11
if (not value.to_s.match(/\A\d{11}\z/) and not value.to_s.match(/\A\d{3}\.\d{3}\.\d{3}\-\d{2}\z/)) or not Cpf.valid?(value)
record.errors.add(attr_name)
end
else
if (not value.to_s.match(/\A\d{14}\z/) and not value.to_s.match(/\A\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}\z/)) or not Cnpj.valid?(value)
record.errors.add(attr_name)
end
end
end
|