udokmeci/yii2-phone-validator

基于PhoneNumberUtil库的Yii2 Phone validator包装器,也由Android设备使用

1.0.4 2024-07-26 12:00 UTC

This package is auto-updated.

Last update: 2024-08-26 12:11:25 UTC


README

Yii2 phone validator是一个使用电话号码工具库来验证和格式化模型中电话号码属性的验证器。

如何使用?

##使用Composer安装 只需在您的composer.json文件中的require对象下添加以下行。

{
  "require": {
    "udokmeci/yii2-phone-validator" : "~1.0.4"
  }
}

然后运行

$> composer update

##配置 现在请将以下内容添加到您的model规则中。 ###注意:国家属性需要ISO 3166-1 alpha-2代码。您可以使用db-regions获取国家列表。

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
          [['name', 'country'], 'string', 'max' => 50],
          // add this line
          [['phone'], 'udokmeci\yii2PhoneValidator\PhoneValidator'],
        ];
    }

##高级 如果未指定countrycountryAttribute,则尝试使用countrycountry_code属性。

  // All phones will be controlled according to Turkey and formatted to TR Phone Number
  [['phone'], 'udokmeci\yii2PhoneValidator\PhoneValidator','country'=>'TR'],// 

  //All phones will be controlled according to value of $model->country_code
  [['phone'], 'udokmeci\yii2PhoneValidator\PhoneValidator','countryAttribute'=>'country_code'],

  //All phones will be controlled according to value of $model->country_code
  //If model has not a country attribute then phone will not be validated
  //If phone is a valid one will be formatted for International Format. default behavior.
  [['phone'], 'udokmeci\yii2PhoneValidator\PhoneValidator','countryAttribute'=>'country_code','strict'=>false,'format'=>true],  

欢迎任何分支。