borales/yii2-phone-input Yii2 International telephone numbers - Asset Bundle, Behavior, Validator, Widget

intl-tel-inputlibphonenumberinternationaltelephonenumbers

Yii2 International telephone numbers - Asset Bundle, Behavior, Validator, Widget

Latest Stable Version Total Downloads Latest Unstable Version License Build Status

This extension uses 2 libraries:

Original demo can be found here - http://jackocnr.com/intl-tel-input.html.

Installation

The preferred way to install this extension is through composer.

Either run

$ php composer.phar require "borales/yii2-phone-input" "*"

or add

"borales/yii2-phone-input": "*"

to the require section of your composer.json file.

Usage

Phone input

Using as an ActiveField widget with the preferred countries on the top:

use borales\extensions\phoneInput\PhoneInput;

echo $form->field($model, 'phone_number')->widget(PhoneInput::className(), [
    'jsOptions' => [
        'preferredCountries' => ['no', 'pl', 'ua'],
    ]
]);

Using as a simple widget with the limited countries list:

use borales\extensions\phoneInput\PhoneInput;

echo PhoneInput::widget([
    'name' => 'phone_number',
    'jsOptions' => [
        'allowExtensions' => true,
        'onlyCountries' => ['no', 'pl', 'ua'],
    ]
]);

Using phone validator in a model (validates the correct country code and phone format):

namespace frontend\models;

use borales\extensions\phoneInput\PhoneInputValidator;

class Company extends Model
{
    public $phone;

    public function rules()
    {
        return [
            [['phone'], 'string'],
            [['phone'], PhoneInputValidator::className()],
        ];
    }
}

or if you need to validate phones of some countries:

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):

namespace frontend\models;

use borales\extensions\phoneInput\PhoneInputBehavior;

class Company extends Model
{
    public $phone;

    public function behaviors()
    {
        return [
            'phoneInput' => PhoneInputBehavior::className(),
        ];
    }
}

You can also thanks to this behavior save to database country code of the phone number. Just add your attribute as countryCodeAttribute and it'll be inserted into database with the phone number.

namespace frontend\models;

use borales\extensions\phoneInput\PhoneInputBehavior;

class Company extends Model
{
    public $phone;
    public $countryCode;

    public function behaviors()
    {
        return [
            [
                'class' => PhoneInputBehavior::className(),
                'countryCodeAttribute' => 'countryCode',
            ],
        ];
    }
}

Note: nationalMode option is very important! In case if you want to manage phone numbers with country/operator code

  • you have to set nationalMode: false in widget options (for example, PhoneInput::widget(...options, ['jsOptions' => ['nationalMode' => false]])).

Changelog

CHANGELOG

0.1.2 (work in progress...)

  • Fix #34: bower-asset/intl-tel-input composer dependency (via asset-packagist.org repository)

0.1.1 (June 11, 2017)

  • Enh: Using bower-asset/intl-tel-input: "^11" dependency

0.1.0 (June 10, 2017)

  • Enh: Using bower-asset/intl-tel-input: "^10" dependency
  • Enh: Using giggsey/libphonenumber-for-php: "^8" dependency
  • Enh #22: Updgrading libphonenumber-for-php library to the latest major release (v8)
  • Enh #16: Validating phone number by type (thanks to @andrey82k)
  • Bug #17: Fixing PhoneInputValidator to work with unique validation rule

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)

0.0.3 (March 16, 2016)

  • Added client validation (thanks to @wkf928592)

0.0.2 (March 04, 2016)

  • Fixing assets paths (thanks to @lars-t)
  • Adding docs

0.0.1 (June 10, 2015)

  • Initial release

Statistics

Downloads
GitHub Stars
GitHub Forks

Releases

Comments



0.3.0 is the latest of 10 releases



BSD-3-Clause license
Stats
97 github stars & 38 github forks
471 downloads in the last day
9794 downloads in the last 30 days
218461 total downloads