25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/validates_and_formats_phones/validates_and_formats_phones.rb', line 25
def validates_and_formats_phones(*args)
formats, fields, options = ValidatesAndFormatsPhones.extract_formats_and_fields(args)
size_options = formats.collect {|format| format.count '#'}
validates_each(*fields) do |record, attr, value|
unless value.blank? || size_options.include?(value.scan(/\d/).size)
if size_options.size > 1
last = size_options.pop
message = "must have #{size_options.join(', ')} or #{last} digits."
else
message = "must have #{size_options[0]} digits."
end
record.errors.add attr, message
else
record.format_phone_field(attr, formats)
end
end
end
|