Class: Phonofy::Validators::ExtendedPhoneValidator

Inherits:
PhoneValidator
  • Object
show all
Defined in:
lib/validators/extended_phone_validator.rb

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/extended_phone_validator.rb', line 10

def validate_each(record, attribute, value)
  return if options[:allow_blank] && value.blank?

  @phone = parse(value, specified_country(record))
  valid  = countries.present? ? valid_country? : phone_valid?
  valid  = valid && valid_types? && valid_extensions?

  unless valid
    # Rails 8.0+ compatibility - errors.add no longer accepts options as a third argument
    if ActiveModel.version >= Gem::Version.new('8.0.0')
      # For Rails 8.0+, use the new API
      record.errors.add(attribute, message(error_message), **options)
    else
      # For older Rails versions, use the old API
      record.errors.add(attribute, message(error_message), options)
    end
  end
end