├── README.md ├── composer.json └── src └── udokmeci └── yii2PhoneValidator └── PhoneValidator.php /README.md: -------------------------------------------------------------------------------- 1 | yii2-phone-validator 2 | ============== 3 | 4 | Yii2 phone validator is a validator uses phone number util to validate and format the phone number attribute of model. 5 | 6 | 7 | How to use? 8 | ============== 9 | ##Installation with Composer 10 | Just add the line under `require` object in your `composer.json` file. 11 | ``` json 12 | { 13 | "require": { 14 | "udokmeci/yii2-phone-validator" : "~1.0.4" 15 | } 16 | } 17 | ``` 18 | then run 19 | 20 | ``` console 21 | $> composer update 22 | ``` 23 | 24 | ##Configuration 25 | Now add following in to your `model` rules. 26 | ###Note: ISO 3166-1 alpha-2 codes are required for country attribute. You can use [db-regions](https://github.com/udokmeci/db-regions) for countries list. 27 | 28 | ``` php 29 | /** 30 | * @inheritdoc 31 | */ 32 | public function rules() 33 | { 34 | return [ 35 | [['name', 'country'], 'string', 'max' => 50], 36 | // add this line 37 | [['phone'], 'udokmeci\yii2PhoneValidator\PhoneValidator'], 38 | ]; 39 | } 40 | ``` 41 | ##Advanced 42 | The `country` and `country_code` attributes are tried if `country` or `countryAttribute` is not specified. 43 | 44 | ``` php 45 | // All phones will be controlled according to Turkey and formatted to TR Phone Number 46 | [['phone'], 'udokmeci\yii2PhoneValidator\PhoneValidator','country'=>'TR'],// 47 | 48 | //All phones will be controlled according to value of $model->country_code 49 | [['phone'], 'udokmeci\yii2PhoneValidator\PhoneValidator','countryAttribute'=>'country_code'], 50 | 51 | //All phones will be controlled according to value of $model->country_code 52 | //If model has not a country attribute then phone will not be validated 53 | //If phone is a valid one will be formatted for International Format. default behavior. 54 | [['phone'], 'udokmeci\yii2PhoneValidator\PhoneValidator','countryAttribute'=>'country_code','strict'=>false,'format'=>true], 55 | 56 | ``` 57 | 58 | Any forks are welcome. 59 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "udokmeci/yii2-phone-validator", 3 | "type": "validator", 4 | "description": "Yii2 Phone validator wrapper on top of PhoneNumberUtil library also used by Android devices", 5 | "keywords": ["phone number","phone number validator","yii2"], 6 | "homepage": "https://github.com/udokmeci/yii2-beanstalk", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Uğur DÖKMECİ", 11 | "email": "ugurdokmeci@gmail.com", 12 | "homepage": "https://github.com/udokmeci/yii2-phone-validator", 13 | "role": "Developer" 14 | } 15 | ], 16 | "support": { 17 | "email": "ugurdokmeci@gmail.com", 18 | "source": "https://github.com/udokmeci/yii2-phone-validator" 19 | }, 20 | "require": { 21 | "php": ">=5.3.0", 22 | "yiisoft/yii2": "*", 23 | "giggsey/libphonenumber-for-php": "~8.0" 24 | }, 25 | "autoload": { 26 | "psr-0" : { 27 | "udokmeci\\yii2PhoneValidator" : "src" 28 | } 29 | }, 30 | "extra": { 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/udokmeci/yii2PhoneValidator/PhoneValidator.php: -------------------------------------------------------------------------------- 1 | format === true) { 32 | $this->format = PhoneNumberFormat::INTERNATIONAL; 33 | } 34 | // if countryAttribute is set 35 | if (!isset($country) && isset($this->countryAttribute)) { 36 | $countryAttribute = $this->countryAttribute; 37 | $country = $model->$countryAttribute; 38 | } 39 | 40 | // if country is fixed 41 | if (!isset($country) && isset($this->country)) { 42 | $country = $this->country; 43 | } 44 | 45 | // if none select from our models with best effort 46 | if (!isset($country) && isset($model->country_code)) 47 | $country = $model->country_code; 48 | 49 | if (!isset($country) && isset($model->country)) 50 | $country = $model->country; 51 | 52 | 53 | // if none and strict 54 | if (!isset($country) && $this->strict) { 55 | $this->addError($model, $attribute, \Yii::t('udokmeci.phone.validator', 'For phone validation country required')); 56 | return false; 57 | } 58 | 59 | if (!isset($country)) { 60 | return true; 61 | } 62 | 63 | $phoneUtil = PhoneNumberUtil::getInstance(); 64 | try { 65 | $numberProto = $phoneUtil->parse($model->$attribute, $country); 66 | if ($phoneUtil->isValidNumber($numberProto)) { 67 | if (is_numeric($this->format)) { 68 | $model->$attribute = $phoneUtil->format($numberProto, $this->format); 69 | } 70 | return true; 71 | } else { 72 | $this->addError($model, $attribute, \Yii::t('udokmeci.phone.validator', 'Phone number does not seem to be a valid phone number')); 73 | return false; 74 | } 75 | } catch (NumberParseException $e) { 76 | $this->addError($model, $attribute, \Yii::t('udokmeci.phone.validator', 'Unexpected Phone Number Format')); 77 | } catch (Exception $e) { 78 | $this->addError($model, $attribute, \Yii::t('udokmeci.phone.validator', 'Unexpected Phone Number Format or Country Code')); 79 | } 80 | } 81 | 82 | } 83 | --------------------------------------------------------------------------------