kublermdk/yii2-phone-validator

基于PhoneNumberUtil库的Yii2 Phone validator包装器,该库也被Android设备使用。这是对https://github.com/udokmeci/yii2-phone-validator的分支,并更新到giggsey/libphonenumber-for-php版本

安装量: 4,018

依赖关系: 0

建议者: 0

安全: 0

星级: 0

关注者: 0

分支: 17

类型:validator

1.0.7 2022-05-26 13:11 UTC

This package is auto-updated.

Last update: 2024-09-26 18:13:10 UTC


README

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

这是https://github.com/udokmeci/yii2-phone-validator的一个略微修改版本,但使用了最新的giggsey/libphonenumber-for-php

创建于2022年第二季度,主要目的是在composer.json的requires行中添加>=,即"giggsey/libphonenumber-for-php": ">=8.0"

这意味着支持更新的电话号码,因为giggsey/libphonenumber-for-php已更新到v8.12.48,而截至2022年5月26日,原始的udokmeci/yii2-phone-validator还停留在v7

注意:如果您使用的是旧版本,您需要将udokmeci\yii2-phone-validator更改为kublermdk\yii2PhoneValidator

如何使用?

##使用Composer安装只需在您的composer.json文件中的require对象下添加一行。如果您遇到Composer找不到仓库的问题,请尝试添加repositories条目,以便它在Github上知道在哪里查找

{
    "require": {
        "kublermdk/yii2-phone-validator" : ">=1.0"
    },
    "repositories": [
      {
        "type": "git",
        "url": "https://github.com/kublermdk/yii2-phone-validator"
      }
    ]
}

然后运行

$> composer update

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

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

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

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

  //All phones will be controlled according to value of $model->country_code
  [['phone'], 'kublermdk\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'], 'kublermdk\yii2PhoneValidator\PhoneValidator','countryAttribute'=>'country_code','strict'=>false,'format'=>true],  

欢迎任何分支。