Class: PhoneValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- PhoneValidator
- Includes:
- Phonelib::Core
- Defined in:
- lib/validators/phone_validator.rb,
lib/validators/phone_validator3.rb
Overview
Validator class for phone validations
Examples
Validates that attribute is a valid phone number. If empty value passed for attribute it fails.
class Phone < ActiveRecord::Base
attr_accessible :number
validates :number, phone: true
end
Validates that attribute is a possible phone number. If empty value passed for attribute it fails.
class Phone < ActiveRecord::Base
attr_accessible :number
validates :number, phone: { possible: true }
end
Validates that attribute is a valid phone number. Empty value is allowed to be passed.
class Phone < ActiveRecord::Base
attr_accessible :number
validates :number, phone: { allow_blank: true }
end
Validates that attribute is a valid phone number of specified type(s). It is also possible to check that attribute is a possible number of specified type(s). Symbol or array accepted.
class Phone < ActiveRecord::Base
attr_accessible :number, :mobile
validates :number, phone: { types: [:mobile, :fixed], allow_blank: true }
validates :mobile, phone: { possible: true, types: :mobile }
end
validates that phone is valid and it is from specified country or countries
class Phone < ActiveRecord::Base
attr_accessible :number
validates :number, phone: { countries: [:us, :ca] }
end
Validates that attribute does not include an extension. The default setting is to allow extensions
class Phone < ActiveRecord::Base
attr_accessible :number
validates :number, phone: { extensions: false }
end
Instance Method Summary collapse
-
#validate_each(record, attribute, value) ⇒ Object
Validation method.
Methods included from Phonelib::Core
#add_additional_regex, #additional_regexes, #additional_regexes=, #default_country, #default_country=, #dump_additional_regexes, #eager_load!, #extension_separate_symbols, #extension_separate_symbols=, #extension_separator, #extension_separator=, #ignore_plus, #ignore_plus=, #impossible?, #invalid?, #invalid_for_country?, #override_phone_data, #override_phone_data=, #parse, #parse_special, #parse_special=, #phone_data, #possible?, #sanitize_regex, #sanitize_regex=, #skip_eager_loading!, #strict_check, #strict_check=, #strict_double_prefix_check, #strict_double_prefix_check=, #valid?, #valid_for_country?, #vanity_conversion, #vanity_conversion=
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
Validation method
59 60 61 62 63 64 65 66 |
# File 'lib/validators/phone_validator.rb', line 59 def validate_each(record, attribute, value) return if [:allow_blank] && value.blank? @phone = parse(value, specified_country(record)) valid = phone_valid? && valid_types? && valid_country? && valid_extensions? record.errors.add(attribute, , **) unless valid end |