Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,all.
I do is extending the CValidator class,
when run , Undefined variable: pattern error.
where problem?


/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('password', 'ext.MyValidators.passwordStrength', 'strength'=>self::STRONG),
);
}



class passwordStrength extends CValidator
{
public $strength;

private $weak_pattern='/^(?=.*[a-zA-Z0-9]).{5,}$/';
private $strong_pattern= '/^(?=.*\d(?=.*\d))(?=.*[a-zA-Z](?=.*[a-zA-Z])).{5,}$/';


/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param CModel $object the object being validated
* @param string $attribute the attribute being validated
*/
protected function validateAttribute($object,$attribute)
{

// check the strength parameter used in the validation rule of our model
if ($this->strength == 'weak')
$pattern = $this->weak_pattern;
elseif ($this->strength == 'strong')
$pattern = $this->strong_pattern;

// extract the attribute value from it's model object
$value=$object->$attribute;

if(!preg_match($pattern, $value))
{
$this->addError($object,$attribute,'your password is too weak!');
}
}

/**
* Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script.
* @see CActiveForm::enableClientValidation
*/
public function clientValidateAttribute($object,$attribute)
{

// check the strength parameter used in the validation rule of our model
if ($this->strength == 'weak')
$pattern = $this->weak_pattern;
elseif ($this->strength == 'strong')
$pattern = $this->strong_pattern;

$condition="!value.match({$pattern})";

return "
if(".$condition.") {
messages.push(".CJSON::encode('your password is too weak, you fool!').");
}";
}

}



Thanks.
Posted
Updated 28-Aug-14 19:31pm
v2
Comments
Sergey Alexandrovich Kryukov 29-Aug-14 9:56am    
How about using the debugger, to see what variable, in what line..?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900