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
 
Installation ¶
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.*"
Usage ¶
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
            array(
                'mobile', 'brval.PhoneValidator',
                'type'     => PhoneValidator::TYPE_CELLPHONE,
                'areaCode' => false
            ),
            // Cellphone validator with external area code check
            array(
                'mobile', 'brval.PhoneValidator',
                'type'              => PhoneValidator::TYPE_CELLPHONE,
                'areaCodeAttribute' => 'areaCode'
            ),
        );
    }
}
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.