igorsantos07/yii-br-pack

此包已被废弃,不再维护。未建议替代包。

为 Yii 1.1.* 提供巴西本地化的验证和易于使用的掩码字段。

安装: 27

依赖: 0

建议者: 1

安全性: 0

星标: 1

关注者: 1

分支: 1

公开问题: 1

类型:yii-extension

v1.2.1 2015-01-26 13:17 UTC

This package is auto-updated.

Last update: 2023-12-28 21:42:15 UTC


README

提供各种辅助工具的 Yii 1.1 扩展,用于巴西本地化。

  • 验证器
    • CPF:个人登记(类似于美国的社保号)
    • CNPJ:全国法人登记
    • 固定电话:以 2 或 3 开头
    • 移动电话:9 位或以 7、8 或 9 开头的 8 位数字
  • 字段
    • CPF 和 CNPJ
    • 多态信用卡字段 - 根据名称变为 PAN、CVV 或过期字段
    • 电话 - 固定电话、移动电话、两者都或仅区号
  • 格式化器
    • CPF
    • 带区号的电话
    • 货币(NumberFormatter::formatCurrency() 的别名,默认为 BRL)

安装

安装此扩展的首选方式是通过 Composer

Latest Stable Version Total Downloads

运行以下命令

php composer.phar require --prefer-dist igorsantos07/yii-br-pack:1.*

或将以下内容添加到您的 composer.json 文件的 "require" 部分。

"igorsantos07/yii-br-pack": "1.*"

使用

按照以下示例添加规则

models/PersonForm.php

class PersonForm extends CModel {

  public $name;

  public $cpf;
  public $cnpj;
  public $cellphone;
  public $landline;
  public $phone;
  public $areaCode;

  public $card_number;
  public $card_cvv;
  public $card_expiry;

  public function rules() {
    // Using short array notation but the class is PHP <5.4 compatible ;)
    return [
      // CPF validator
      ['cpf', 'BrPack\Validator\Cpf'],
      // CNPJ validator
      ['cnpj', 'BrPack\Validator\Cnpj'],
      // Cellphone-only validator, checking area code inside the field
      ['cellphone', 'BrPack\Validator\Phone', 'type' => PhoneValidator::TYPE_CELLPHONE],
      // Cellphone-only validator, not validating area code
      [
        'cellphone',
        'BrPack\Validator\Phone',
        'type'     => BrPack\Validator\Phone::TYPE_CELLPHONE,
        'areaCode' => false
      ],
      // Landline-only validator
      ['landline', 'BrPack\Validator\Phone', 'type' => BrPack\Validator\Phone::TYPE_LANDLINE],
      // Any phone validator - cellphone or landline
      ['phone', 'BrPack\Validator\Phone'],
      // Cellphone validator with external area code check
      [
        'cellphone',
        'BrPack\Validator\Phone',
        'type'              => BrPack\Validator\Phone::TYPE_CELLPHONE,
        'areaCodeAttribute' => 'areaCode'
      ],
    ];
  }
}

views/person/edit.php

<?php $form = $this->beginWidget('CActiveForm, ['id' => 'my-person-form']) ?>

    <?=$form->label($model, 'name')?>
    <?=$form->textField($model, 'name')?>
    <?=$form->error($model, 'name')?>
    <br/>

	Current document: <?=Yii::app()->format->cpf('12365487588')?> // 123.654.875-88
    <?=$form->label($model, 'cpf')?>
    <?php $this->widget('BrPack\Field\Cpf', ['model' => $model, 'attribute' => 'cpf']) ?>
    <?=$form->error($model, 'cpf')?>
    <br/>

	Current phone: <?=Yii::app()->format->phone('21996543354')?> // (21) 99654-3354
    <?=$form->label($model, 'cellphone')?>
    <?php $this->widget('BrPack\Field\Phone', ['model' => $model, 'attribute' => 'cellphone', 'type' => 'mobile']) ?>
    <?=$form->error($model, 'cpf')?>
    <br/>

    <?=$form->label($model, 'card_number')?>
    <?php $this->widget('BrPack\Field\Card', ['model' => $model, 'attribute' => 'card_number']) ?>
    <?php $this->widget('BrPack\Field\Card', ['model' => $model, 'attribute' => 'card_expiry']) ?>
    <?php $this->widget('BrPack\Field\Card', ['model' => $model, 'attribute' => 'card_cvv']) ?>
    <?=$form->error($model, 'card_number')?>
    <?=$form->error($model, 'card_expiry')?>
    <?=$form->error($model, 'card_cvv')?>

    Amount to be charged in your card: <?=Yii::app()->format->money(9.99)?> // R$9,99
    <br/>

    <?=CHtml::submitButton()?>
<?php $this->endWidget() ?>