Hello -
I am using one of the field validators that is based on the CValidator class.
When I pass a custom error message to it I get the error -
Property "cpf.0" is not defined.
How do I pick up the ‘message’ value or allow it to be used to override the built in one???
In my model I use this as the validator code
…
array('int_cpf', 'message'=> 'abc', 'ext.validators.cpf'), // call validate_CPF() to take care of the work, use my custom message
…
The code for the validator class which works fine unless I try to use my message override.
Code for the class -
class cpf extends CValidator
{
// added collection of user supplied properties, mainly the message
public function __construct() {
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} is invalid.');
}
}
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param CModel the data object being validated
* @param string the name of the attribute to be validated.
*/
protected function validateAttribute( $object, $attribute ){
if ( !$this->validaCPF( $object->$attribute ) )
$this->addError($object, $attribute, $this->message);
}
public function clientValidateAttribute($object,$attribute) {
}
/*
* @autor: Moacir Selínger Fernandes
* @email: hassed@hassed.com
* Qualquer dúvida é só mandar um email
* http://codigofonte.uol.com.br/codigo/php/validacao/validacao-de-cpf-com-php
*
* Modificada conforme indicações nos comentários de habner e calex
*/
// Função que valida o CPF
private function validaCPF($cpf)
{ // Verifiva se o número digitado contém todos os digitos
$cpf = str_pad(preg_replace('/[^0-9_]/', '', $cpf), 11, '0', STR_PAD_LEFT);
// valida número sequencial 1111... 22222 ......
for ($x=0; $x<10; $x++)
if ( $cpf == str_repeat($x, 11) )
return false;
// Verifica se nenhuma das sequências abaixo foi digitada, caso seja, retorna falso
if ( strlen($cpf) != 11 )
{
return false;
}
else
{ // Calcula os números para verificar se o CPF é verdadeiro
for ($t = 9; $t < 11; $t++) {
for ($d = 0, $c = 0; $c < $t; $c++) {
$d += $cpf{$c} * (($t + 1) - $c);
}
$d = ((10 * $d) % 11) % 10;
if ($cpf{$c} != $d) {
return false;
}
}
return true;
}
}
}