Coder Social home page Coder Social logo

yii2-phone-input's Introduction

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]])).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.