Class: PhoneValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- PhoneValidator
- Includes:
- Phonelib::Core
- Defined in:
- lib/validators/phone_validator.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
Constant Summary
Constants included from Phonelib::Core
Phonelib::Core::AREA_CODE_MOBILE_COUNTRIES, Phonelib::Core::AREA_CODE_MOBILE_TOKENS, Phonelib::Core::AREA_CODE_OPTIONAL, Phonelib::Core::AREA_CODE_TYPES, Phonelib::Core::CARRIER_SELECTION_CODES, Phonelib::Core::CARRIER_SERVICES, Phonelib::Core::CARRIER_SPECIFIC, Phonelib::Core::COUNTRY_CODE, Phonelib::Core::DEFAULT_NUMBER_FORMAT, Phonelib::Core::DIRECTORY_SERVICES, Phonelib::Core::DOUBLE_COUNTRY_PREFIX_FLAG, Phonelib::Core::EMERGENCY, Phonelib::Core::EXPANDED_EMERGENCY, Phonelib::Core::EXT_CARRIERS, Phonelib::Core::EXT_CARRIER_KEY, Phonelib::Core::EXT_COUNTRY_NAMES, Phonelib::Core::EXT_GEO_NAMES, Phonelib::Core::EXT_GEO_NAME_KEY, Phonelib::Core::EXT_PREFIXES, Phonelib::Core::EXT_TIMEZONES, Phonelib::Core::EXT_TIMEZONE_KEY, Phonelib::Core::FILE_EXT_DATA, Phonelib::Core::FILE_MAIN_DATA, Phonelib::Core::FIXED_LINE, Phonelib::Core::FIXED_OR_MOBILE, Phonelib::Core::FORMATS, Phonelib::Core::GENERAL, Phonelib::Core::INTERNATIONAL_PREFIX, Phonelib::Core::LEADING_DIGITS, Phonelib::Core::MAIN_COUNTRY_FOR_CODE, Phonelib::Core::MOBILE, Phonelib::Core::NATIONAL_PREFIX, Phonelib::Core::NATIONAL_PREFIX_FOR_PARSING, Phonelib::Core::NATIONAL_PREFIX_RULE, Phonelib::Core::NATIONAL_PREFIX_TRANSFORM_RULE, Phonelib::Core::NO_INTERNATIONAL_DIALING, Phonelib::Core::PAGER, Phonelib::Core::PATTERN, Phonelib::Core::PERSONAL_NUMBER, Phonelib::Core::PLUS_SIGN, Phonelib::Core::POSSIBLE_PATTERN, Phonelib::Core::PREMIUM_RATE, Phonelib::Core::SHARED_COST, Phonelib::Core::SHORT, Phonelib::Core::SHORT_CODE, Phonelib::Core::SHORT_CODES, Phonelib::Core::SMS_SERVICES, Phonelib::Core::STANDARD_RATE, Phonelib::Core::TOLL_FREE, Phonelib::Core::TYPES, Phonelib::Core::TYPES_DESC, Phonelib::Core::UAN, Phonelib::Core::VALID_PATTERN, Phonelib::Core::VANITY_4_LETTERS_KEYS_REGEX, Phonelib::Core::VOICEMAIL, Phonelib::Core::VOIP
Instance Method Summary collapse
-
#validate_each(record, attribute, value) ⇒ Object
Validation method.
Methods included from Phonelib::Core
#add_additional_regex, #additional_regexes, #additional_regexes=, #data_by_country_codes, #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, #phone_data_int_prefixes, #phone_ext_data, #phone_regexp_cache, #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
61 62 63 64 65 66 67 68 |
# File 'lib/validators/phone_validator.rb', line 61 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 |