From 722faf1a5d90f7f7dea30d71a85bc7614b8ff12b Mon Sep 17 00:00:00 2001 From: RichWeber Date: Sun, 5 Jun 2016 16:43:11 +0300 Subject: [PATCH] Added validation of countries --- CHANGELOG.md | 4 ++++ README.md | 26 ++++++++++++++++++++++++-- composer.json | 2 +- src/PhoneInputValidator.php | 31 ++++++++++++++++++++++++++++--- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ba8f6f..b4b5c38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 0.0.5 (June 05, 2016) + +- Added validation of countries (thanks to @RichWeber) + ## 0.0.4 (April 12, 2016) - Namespace fix (thanks to @sample-game) diff --git a/README.md b/README.md index 5f28353..72433b1 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ Yii2 International telephone numbers - Asset Bundle, Behavior, Validator, Widget ================================================================================ [![Latest Stable Version](https://poser.pugx.org/borales/yii2-phone-input/v/stable.svg)](https://packagist.org/packages/borales/yii2-phone-input) -[![Total Downloads](https://poser.pugx.org/borales/yii2-phone-input/downloads.svg)](https://packagist.org/packages/borales/yii2-phone-input) -[![Latest Unstable Version](https://poser.pugx.org/borales/yii2-phone-input/v/unstable.svg)](https://packagist.org/packages/borales/yii2-phone-input) +[![Total Downloads](https://poser.pugx.org/borales/yii2-phone-input/downloads.svg)](https://packagist.org/packages/borales/yii2-phone-input) +[![Latest Unstable Version](https://poser.pugx.org/borales/yii2-phone-input/v/unstable.svg)](https://packagist.org/packages/borales/yii2-phone-input) [![License](https://poser.pugx.org/borales/yii2-phone-input/license.svg)](https://packagist.org/packages/borales/yii2-phone-input) This extension uses 2 libraries: @@ -82,6 +82,28 @@ class Company extends Model } ``` +or if you need to validate phones of some countries: + +```php +namespace frontend\models; + +use borales\extensions\phoneInput\PhoneInputValidator; + +class Company extends Model +{ + public $phone; + + public function rules() + { + return [ + [['phone'], 'string'], + // [['phone'], PhoneInputValidator::className(), 'region' => 'UA'], + [['phone'], PhoneInputValidator::className(), 'region' => ['PL', 'UA']], + ]; + } +} +``` + Using phone behavior in a model (auto-formats phone string to the required phone format): ```php diff --git a/composer.json b/composer.json index e8d77f3..a1df970 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "borales/yii2-phone-input", - "version": "0.0.4", + "version": "0.0.5", "description": "Yii2 International telephone numbers - Asset Bundle, Behavior, Validator, Widget", "keywords": [ "yii2", diff --git a/src/PhoneInputValidator.php b/src/PhoneInputValidator.php index 8bc2aa2..de01663 100644 --- a/src/PhoneInputValidator.php +++ b/src/PhoneInputValidator.php @@ -14,6 +14,14 @@ */ class PhoneInputValidator extends Validator { + /** + * @var mixed + */ + public $region; + + /** + * @inheritdoc + */ public function init() { if (!$this->message) { @@ -32,14 +40,31 @@ protected function validateValue($value) $phoneUtil = PhoneNumberUtil::getInstance(); try { $phoneProto = $phoneUtil->parse($value, null); - if ($phoneUtil->isValidNumber($phoneProto)) { - $valid = true; + + if ($this->region !== null) { + if (is_array($this->region)) { + foreach ($this->region as $region) { + if ($phoneUtil->isValidNumberForRegion($phoneProto, $region)) { + $valid = true; + break; + } + } + } else { + if ($phoneUtil->isValidNumberForRegion($phoneProto, $this->region)) { + $valid = true; + } + } + } else { + if ($phoneUtil->isValidNumber($phoneProto)) { + $valid = true; + } } + } catch (NumberParseException $e) { } return $valid ? null : [$this->message, []]; } - + /** * @inheritdoc */