Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Added I18n PhoneNumber validator based off of country #4127

Merged
merged 1 commit into from
Apr 3, 2013
Merged

Added I18n PhoneNumber validator based off of country #4127

merged 1 commit into from
Apr 3, 2013

Conversation

mwillbanks
Copy link
Contributor

I am breaking off items from #3884 to make it easier to be able to merge each individually. Please see original PR for several notes. None were given on phone number implementation.

Phone Number

The Phone Number validator is based on libPhoneNumber's XML file. It is a multi-dimensional hash that takes the country code as a key then several subkeys for possible types, national types and separated by types of numbers. The country is primarily used so that you will only need to validate against a subset of items. A country is required.
Basic Usage:

$validator = new \Zend\I18n\Validator\PhoneNumber();
$validator->setCountry('US'); // could be omitted since AD below is a country
$validator->isValid('2015550123');

// input filter with context
$filter = new InputFilter();
$factory = new InputFactory();
// assumption here is that we have a country field coming through the context
$filter->add($factory->createInput(array(
    'name' => 'country',
    'required' => true,
    'validators' => array(
        'name' => 'Zend\I18n\Validator\Country', // separate PR coming later
    ),
)));
$filter->add($factory->createInput(array(
    'name' => 'phoneNumber',
    'required' => true,
    'filters' => array(
        'Digits',
    ),
    'validators' => array(
        'name' => 'Zend\I18n\Validator\PhoneNumber',
        'options' => array(
            'country' => 'country',
        ),
    ),
)));

@EvanDotPro
Copy link
Member

Thank you. Thank you. Thank you. Thank you.

@mwillbanks
Copy link
Contributor Author

@EvanDotPro i think it may need some additional work to fit your use case a bit better but it should be customizable enough for now. I think it could possibly go through all patterns to find possible matches and then attempt to figure out a country or something like that but for now it requires a country :)

@tca3
Copy link
Contributor

tca3 commented Mar 27, 2013

I am wondering if there isn't a better way to implement a 200kb array in the validator, I was looking at SplFixedArray but that would require a second array for indexes. I wonder what the benchmarks are on memory usage on this PR.

More generally, the question is, is this the best way to use large fixed datasets accross ZF2.

@DASPRiD
Copy link
Member

DASPRiD commented Mar 27, 2013

@mwillbanks About the large array problem @ThomasCantonnet mentioned:

I'd suggest to split the country data into individual files (Zend/I18n/Validator/PhoneNumer/[a-z]{2}.php). and then just load them as required:

$specs = require __DIR__ . '/PhoneNumber/' . $countryCode . '.php';

This should solve a possible memory problem quite a bit.

@mwillbanks
Copy link
Contributor Author

I'll refactor this a bit; likely tonight ;)

@mwillbanks
Copy link
Contributor Author

Rebased, force pushed, adjusted for feedback.
@EvanDotPro @DASPRiD care to review?

@ghost ghost assigned weierophinney Apr 3, 2013
weierophinney added a commit that referenced this pull request Apr 3, 2013
…number

Added I18n PhoneNumber validator based off of country
weierophinney added a commit that referenced this pull request Apr 3, 2013
@weierophinney weierophinney merged commit a730867 into zendframework:develop Apr 3, 2013
@mwillbanks mwillbanks deleted the feature/i18n-validator-phonenumber branch April 4, 2013 22:48
@wohugb
Copy link

wohugb commented Sep 24, 2013

how to set only for mobile?
find it:

'allowed_types' => array('mobile'),

@Thinkscape
Copy link
Member

#5150 is still hanging ...

weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
…eature/i18n-validator-phonenumber

Added I18n PhoneNumber validator based off of country
weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants