未定义的变量:yii中的模式,preg_match [英] Undefined variable: pattern in yii ,preg_match
问题描述
大家好。
我做的是扩展CValidator类,
运行时,未定义变量:模式错误。
哪里有问题?
/ **
* @return模型属性的数组验证规则。
* /
公共职能规则()
{
返回数组(
array('密码','ext.MyValidators.passwordStrength','strength'=> self :: STRONG),
);
}
class passwordStrength扩展CValidator
{
public $ strength; < br $>
private $ weak_pattern ='/ ^(?=。* [a-zA-Z0-9])。{5,} $ /';
private $ strong_pattern ='/^(?=。*。* d(?=。*。* d)(?=。* [ - zA-Z](?=。* [A-ZA-Z])){5} $ /'。
/ **
*验证对象的属性。
*如果有任何错误,则会将错误消息添加到对象中。
* @param CModel $ object正在验证的对象
* @param string $ attribute属性被验证
* /
受保护的函数validateAttribute($ object,$ attribute)
{
//检查模型验证规则中使用的强度参数
if($ this-> strength =='weak')
$ pattern = $ this-> weak_pattern;
elseif($ this-> strength =='strong')
$ pattern = $ this-> strong_pattern;
//从模型对象中提取属性值
$ value = $ object-> $ attribute;
if(!preg_match($ pattern,$ value))
{
$ this-> addEr ror($ object,$ attribute,'你的密码太弱了!');
}
}
/ **
*返回执行客户端验证所需的JavaScript。
* @param CModel $ object要验证的数据对象
* @param string $属性要验证的属性的名称。
* @return字符串客户端验证脚本。
* @see CActiveForm :: enableClientValidation
* /
公共函数clientValidateAttribute($ object,$ attribute)
{
//检查我们模型验证规则中使用的强度参数
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('你的密码太弱,你这个傻瓜!')。 );
};
}
}
谢谢。
力量;
private
weak_pattern ='/ ^(?=。* [a-zA-Z0-9])。{5,}
/';
private
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.
strength;
private
weak_pattern='/^(?=.*[a-zA-Z0-9]).{5,}
/';
private
这篇关于未定义的变量:yii中的模式,preg_match的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!