Skip to content

Commit

Permalink
Merge pull request #12 from RichWeber/master
Browse files Browse the repository at this point in the history
Added validation of countries (by @RichWeber)
  • Loading branch information
Borales committed Jun 5, 2016
2 parents 2a578a2 + 722faf1 commit 1f4eeb6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
31 changes: 28 additions & 3 deletions src/PhoneInputValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
*/
class PhoneInputValidator extends Validator
{
/**
* @var mixed
*/
public $region;

/**
* @inheritdoc
*/
public function init()
{
if (!$this->message) {
Expand All @@ -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
*/
Expand Down

0 comments on commit 1f4eeb6

Please sign in to comment.