![]() |
|
Snippets |
|
A CNPJ and CPF validator for brazilian users...
Just add this in the module/lib/myCpfValidator.class.php path...
//Symfony integration as sfValidator of CPF and //CNPJ //Date - Jul 11, 2006 //Author - Lucas Peres (mysyfy@gmail.com) //CNPJ and CPF Validator //Author: Marcelo Bom Jardim //ABOUT //This PHP script will validate CNPJ and CPF //number by checking there length //and some other validations. class myCpfValidator extends sfValidator { private function validaCPF($cpf) { $soma = 0; if (strlen($cpf) <> 11) return false; // Verifica 1º digito for ($i = 0; $i < 9; $i++) { $soma += (($i+1) * $cpf[$i]); } $d1 = ($soma % 11); if ($d1 == 10) { $d1 = 0; } $soma = 0; // Verifica 2º digito for ($i = 9, $j = 0; $i > 0; $i--, $j++) { $soma += ($i * $cpf[$j]); } $d2 = ($soma % 11); if ($d2 == 10) { $d2 = 0; } if ($d1 == $cpf[9] && $d2 == $cpf[10]) { return true; } else { return false; } } private function validaCNPJ($cnpj) { if (strlen($cnpj) <> 14) return false; $soma = 0; $soma += ($cnpj[0] * 5); $soma += ($cnpj[1] * 4); $soma += ($cnpj[2] * 3); $soma += ($cnpj[3] * 2); $soma += ($cnpj[4] * 9); $soma += ($cnpj[5] * 8); $soma += ($cnpj[6] * 7); $soma += ($cnpj[7] * 6); $soma += ($cnpj[8] * 5); $soma += ($cnpj[9] * 4); $soma += ($cnpj[10] * 3); $soma += ($cnpj[11] * 2); $d1 = $soma % 11; $d1 = $d1 < 2 ? 0 : 11 - $d1; $soma = 0; $soma += ($cnpj[0] * 6); $soma += ($cnpj[1] * 5); $soma += ($cnpj[2] * 4); $soma += ($cnpj[3] * 3); $soma += ($cnpj[4] * 2); $soma += ($cnpj[5] * 9); $soma += ($cnpj[6] * 8); $soma += ($cnpj[7] * 7); $soma += ($cnpj[8] * 6); $soma += ($cnpj[9] * 5); $soma += ($cnpj[10] * 4); $soma += ($cnpj[11] * 3); $soma += ($cnpj[12] * 2); $d2 = $soma % 11; $d2 = $d2 < 2 ? 0 : 11 - $d2; if ($cnpj[12] == $d1 && $cnpj[13] == $d2) { return true; } else { return false; } } public function execute (&$value, &$error) { if($this->getParameter('tipo') == 'cpf') { if(!$this->validaCPF($value)) { $error = $this->getParameter('msg_error'); return false; } } else if($this->getParameter('tipo') == 'cnpj') { if(!$this->validaCNPJ($value)) { $error = $this->getParameter('msg_error'); return false; } } else { $error = $this->getParameter('msg_error'); return false; } return true; } public function initialize ($context, $parameters = null) { // initialize parent parent::initialize($context, $parameters); // set defaults $this->setParameter('msg_error', 'Invalid input'); $this->getParameterHolder()->add($parameters); return true; } }
Then at the module/validate.yml add:
names: [...] cpf: required: Yes required_msg: O campo CPF é requerido validators: cpfValidator cpfValidator: class: myCpfValidator param: tipo: cpf msg_error: CPF inválido
Comments on this snippet
Thank you very much for providing the code. At the moment I am working for a Brazilian customer and that is exactly what I need.