yjballestero/yii2-phone-input

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

dev-master 2024-06-12 16:07 UTC

This package is auto-updated.

Last update: 2024-06-12 16:07:29 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build Status

Requirements

This extension uses:

  • PHP 8.1+.
  • Yii2 2.0.45+

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 "yjballestero/yii2-phone-input" "*"

or add

"yjballestero/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 yjballestero\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 yjballestero\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 yjballestero\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 yjballestero\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 yjballestero\phoneInput\PhoneInputBehavior;

class Company extends Model
{
    public $phone;

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

Thanks to this behavior, you can also save the country code of the phone number in the database. Just add your attribute like countryCodeAttribute and it will be inserted into the database with the phone number.

namespace frontend\models;

use yjballestero\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]])).