![]() |
|
Snippets |
|
This its a password strength validator, with ajax request for checking the password field.
First create a validator in lib/validators/sfPasswordStrengthValidator.class.php
<?php class sfPasswordStrengthValidator extends sfValidator { public function execute (&$value, &$error) { $weakness = $this->Password_Strength($value); if($weakness==1) { $error = $this->getParameter('strength_error'); return false; } return $weakness; } public function initialize ($context, $parameters = null) { // Initialize parent parent::initialize($context); // Set default parameters value $this->setParameter('strength_error', 'Weak password'); // Set parameters $this->getParameterHolder()->add($parameters); return true; } // Thanks for: Alix Axel Weblog // URL: http://www.alixaxel.com/wordpress/wp-content/2007/06/Password_Strength.phps function Password_Strength($password, $username = null) { if (!empty($username)) { $password = str_replace($username, '', $password); } $strength = 0; $password_length = strlen($password); if ($password_length < 5) { return $strength; } else { $strength = $password_length * 4; } for ($i = 2; $i <= 4; $i++) { $temp = str_split($password, $i); $strength -= (ceil($password_length / $i) - count(array_unique($temp))); } preg_match_all('/[0-9]/', $password, $numbers); if (!empty($numbers)) { $numbers = count($numbers[0]); if ($numbers >= 3) { $strength += 5; } } else { $numbers = 0; } preg_match_all('/[|!@#$%&*\/=?,;.:\-_+~^¨\\\]/', $password, $symbols); if (!empty($symbols)) { $symbols = count($symbols[0]); if ($symbols >= 2) { $strength += 5; } } else { $symbols = 0; } preg_match_all('/[a-z]/', $password, $lowercase_characters); preg_match_all('/[A-Z]/', $password, $uppercase_characters); if (!empty($lowercase_characters)) { $lowercase_characters = count($lowercase_characters[0]); } else { $lowercase_characters = 0; } if (!empty($uppercase_characters)) { $uppercase_characters = count($uppercase_characters[0]); } else { $uppercase_characters = 0; } if (($lowercase_characters > 0) && ($uppercase_characters > 0)) { $strength += 10; } $characters = $lowercase_characters + $uppercase_characters; if (($numbers > 0) && ($symbols > 0)) { $strength += 15; } if (($numbers > 0) && ($characters > 0)) { $strength += 15; } if (($symbols > 0) && ($characters > 0)) { $strength += 15; } if (($numbers == 0) && ($symbols == 0)) { $strength -= 10; } if (($symbols == 0) && ($characters == 0)) { $strength -= 10; } if ($strength < 0) { $strength = 0; } if ($strength > 100) { $strength = 100; } return $strength; } }
Then in your view template use:
<?php echo observe_field('password', array( 'update' => 'passwordStatus', 'url' => 'sfGuardAuth/checkPasswordStrength', 'with' => "'id='+encodeURIComponent($('password').value)", 'loading'=>"Element.show('indicator_passwordstatus')", 'complete'=>"Element.hide('indicator_passwordstatus');Element.show('passwordStatus');", )) ?>
That code will monitor changes at password input field, and submit the updated value to the defined route.
Then add the following to your actions file:
public function executeCheckPasswordStrength() { $password = $this->getRequestParameter('id'); $strengthValidator = new sfPasswordStrengthValidator(); $strengthValidator->initialize($this->getContext()); $error='none'; $score = $strengthValidator->execute($password,$error); if(!$score) return $this->renderText('too short'); if($score < 20) { return $this->renderText('not weak'); } else if($score < 50) { return $this->renderText('relevant'); } else { return $this->renderText('strong'); } return true; }
Cheers
Currently i had to do different validations of data depending the data entered by the user.
The solution i found, using the symfony built-in yml validation was:
action:
$validated = true; $validationConfig = $this->getModuleName().'/'.sfConfig::get('sf_app_module_validate_dir_name').'/'.$validationFile.'.yml'; if (null !== $validateFile = sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$validationConfig, true)) { $context = $this->getContext(); $validatorManager = new sfValidatorManager(); $validatorManager->initialize($context); require($validateFile); $validated = $validatorManager->execute(); }
refactoring:
public function validateParamsYml($validationFile) { $validated = true; $validationConfig = $this->getModuleName().'/'.sfConfig::get('sf_app_module_validate_dir_name').'/'.$validationFile.'.yml'; if (null !== $validateFile = sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$validationConfig, true)) { $context = $this->getContext(); $validatorManager = new sfValidatorManager(); $validatorManager->initialize($context); require($validateFile); $validated = $validatorManager->execute(); } return $validated; }
Then you can call any validations rules at your action:
public function executeXXX() { [...] [...] $this->validateParamsYml('yml_1'); $this->validateParamsYml('yml_2'); [...] }
and finally check if everything was validated then forward/save the data:
if($this->getRequest()->hasErrors()) { return $this->handleErrorXXXX(); }
Cheers
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