PHP code example of udokmeci / yii2-phone-validator

1. Go to this page and download the library: Download udokmeci/yii2-phone-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

udokmeci / yii2-phone-validator example snippets


use udokmeci\yii2PhoneValidator\PhoneValidator;

class Contact extends ActiveRecord
{
    public function rules()
    {
        return [
            [['phone'], PhoneValidator::class, 'country' => 'NL'],
        ];
    }
}

use udokmeci\yii2PhoneValidator\PhoneValidator;
use udokmeci\yii2PhoneValidator\PhoneNumberFormat;

// Modern PHP 8.1+ enum usage
public function rules()
{
    return [
        [['phone'], PhoneValidator::class, 
            'country' => 'NL',
            'format' => PhoneNumberFormat::E164        // Enum case
        ],
        [['mobile'], PhoneValidator::class,
            'country' => 'NL', 
            'format' => PhoneNumberFormat::INTERNATIONAL // Enum case
        ],
    ];
}

public function rules()
{
    return [
        [['phone'], PhoneValidator::class, 'countryAttribute' => 'country_code'],
    ];
}

use udokmeci\yii2PhoneValidator\PhoneNumberFormat;

public function rules()
{
    return [
        [['phone'], PhoneValidator::class, 
            'country' => 'NL',
            'format' => PhoneNumberFormat::E164  // Using enum
        ],
    ];
}

// Input: '0612345678'
// Output: '+31 6 12345678'

[['phone'], PhoneValidator::class, 'country' => 'US']
// Input: '2125551234' 
// Output: '+1 212 555 1234'

[['phone'], PhoneValidator::class, 'strict' => false]
// Validates only if country is available