yii-br-validator Yii 1.1 extension that provides validation for Brazilian documents (CPF and CNPJ), landlines and cellphones.

  1. Yii Brazilian Validators
  2. Resources

Yii Brazilian Validators

Yii 1.1 Extension that provides validators for Brazilian localization.

  • CPF: Cadastro de Pessoa Física (like a Security Social Number in USA)
  • CNPJ: Cadastro Nacional de Pessoa Jurídica
  • landlines: beginning with 2 and 3
  • cellphones: 9 digits or 8 digits beginning with 7, 8 or 9

The preferred way to install this extension is through Composer.

Either run

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

or add this to the "require" section of your composer.json file.

"yiibr/yii-br-validator": "1.*"

Add the rules as the following example:

// Using an alias in your config file would make it easier to write the validators :)
// Here we are assuming you have at least an alias for your vendor folder.
Yii::setPathOfAlias('brval', Yii::getPathOfAlias('vendor.igorsantos07.yii-br-validators');
class PersonForm extends CModel {

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

    public function rules() {
        return array(
            // CPF validator
            array('cpf', 'brval.CpfValidator'),
            // CNPJ validator
            array('cnpj', 'brval.CnpjValidator'),
            // Any phone validator - cellphone or landline
            array('phone', 'brval.PhoneValidator'),
            // Landline-only validator
            array('landline', 'brval.PhoneValidator', 'type' => PhoneValidator::TYPE_LANDLINE),
            // Cellphone-only validator, checking area code inside the field
            array('mobile', 'brval.PhoneValidator', 'type' => PhoneValidator::TYPE_CELLPHONE),
            // Cellphone-only validator, not validating area code
                'mobile', 'brval.PhoneValidator',
                'type'     => PhoneValidator::TYPE_CELLPHONE,
                'areaCode' => false
            // Cellphone validator with external area code check
                'mobile', 'brval.PhoneValidator',
                'type'              => PhoneValidator::TYPE_CELLPHONE,
                'areaCodeAttribute' => 'areaCode'


0 0
1 follower
Yii Version: 1.1
License: BSD-2-Clause
Category: Validation
Developed by: igorsantos07
Created on: May 17, 2014
Last updated: 9 years ago


show all

Related Extensions