floor12/yii2-phone Yii2 phone formatter and validator.


  1. Installation
  2. Usage

Formatter and validator form phone numbers

Этот файл доступен на русском языке.

Build Status Code quality score Code Coverage Latest Stable Version Total Downloads License


To use this extension run this command:

`bash $ composer require floor12/yii2-phone or add this to the `require` section of your composer.json. json "floor12/yii2-phone": "dev-master" `


This extension allows to validate phone numbers and save only numbers in db without any formatting. It also include simple formatter to render formatted phone numbers as string or html <a href='tel:'> tag.

Phone validation

To store phone number in database, ActiveRecord model database field should be VARCHAR(15).

The validator has backend and frontend (js) validation. To validate your field, add floor12\phone\PhoneValidator to ActiveRecord::rules() action like this:

use floor12\phone\PhoneValidator;
use yii\base\Model;

class User extends Model

    public $phone;

    public function rules()
        return [
            ['phone', PhoneValidator::class]
Phone formatting

Class floor12\phone\PhoneFormatter allows to render phone number as formatted string or as html <a href="tel:">, and has two static methods:

  • PhoneFormatter::format($phone)
  • PhoneFormatter::a($phone,array $options= [])

Formatting examples

echo PhoneFormatter::format(79461234565);                       # +7 (946) 123-45-65
echo PhoneFormatter::a(79461234565);                            # <a href='tel:+79461234565'>+7 (946) 123-45-65</a>
echo PhoneFormatter::a(79461234565,['class'=>'phone-link']);    # <a href='tel:+79461234565' class='phone-link'>+7 (946) 123-45-65</a>
0 0
1 follower
2 535 downloads
Yii Version: 2.0
License: MIT
Category: Validation
Developed by: floor12
Created on: Jul 18, 2022
Last updated: (not set)
Packagist Profile
Github Repository

Related Extensions