PHP YII框架开发小技巧之模型(models)中rules自定义验证规则
YII的models中的rules部分是一些表单的验证规则,对于表单验证十分有用,在相应的视图(views)里面添加了表单,在表单被提交之前程序都会自动先来这里面的规则里验证,只有通过对其有效的限制规则后才能被提交,可以很有效地保证表单安全和信息的有效性。还是给大家具体说明一下:
以下是视图(views)部分的简单代码:
<?php$form=$this->beginWidget('CActiveForm',array( 'id'=>'tag-form', 'enableAjaxValidation'=>false, ));?> <divclass="row"> <?phpecho$form->labelEx($model,'tagname');?> <?phpecho$form->textField($model,'tagname',array('size'=>20,'maxlength'=>32));?> </div> <divclass="row"> <?phpecho$form->labelEx($model,'tagtype');?> <?phpecho$form->radioButtonList($model,'tagtype'array(1=>"普通TAG",2=>"系统默认TAG"),array('separator'=>'','labelOptions'=>array('class'=>'tagtypelabel')));?> </div> <?phpecho$form->errorSummary($model);?> <divclass="rowbuttons"> <?phpechoCHtml::submitButton($model->isNewRecord?'添加':'修改');?> </div> <?php$this->endWidget();?>
模型(models)中rules部分的简单代码:
publicfunctionrules() { returnarray( array('tagname,tagtype','required'), array('tagtype','numerical','integerOnly'=>true), array('tagname','length','max'=>32), array('tagname','match','pattern'=>'/^[\x{4e00}-\x{9fa5}A-Za-z0-9]+$/u', 'message'=>'标签不合法,必须为汉字、字母或者数字!'), array('tagname','checktagname','on'=>'create,update'),//插入TAG时检查是否已经存在该tag array('tagid,tagname,tagtype','safe','on'=>'search'), ); }
系统默认有这些验证规则:
boolean:CBooleanValidator的别名,确保属性的值是CBooleanValidator::trueValue或CBooleanValidator::falseValue.
captcha:CCaptchaValidator的别名,确保了特性的值等于CAPTCHA显示出来的验证码.
compare:CCompareValidator的别名,确保了特性的值等于另一个特性或常量.
email:CEmailValidator的别名,确保了特性的值是一个有效的电邮地址.
default:CDefaultValueValidator的别名,为特性指派了一个默认值.
exist:CExistValidator的别名,确保属性值存在于指定的数据表字段中.
file:CFileValidator的别名,确保了特性包含了一个上传文件的名称.
filter:CFilterValidator的别名,使用一个filter转换属性.
in:CRangeValidator的别名,确保了特性出现在一个预订的值列表里.
length:CStringValidator的别名,确保了特性的长度在指定的范围内.
match:CRegularExpressionValidator的别名,确保了特性匹配一个正则表达式.
numerical:CNumberValidator的别名,确保了特性是一个有效的数字.
required:CRequiredValidator的别名,确保了特性不为空.
type:CTypeValidator的别名,确保了特性为指定的数据类型.
unique:CUniqueValidator的别名,确保了特性在数据表字段中是唯一的.
url:CUrlValidator的别名,确保了特性是一个有效的路径.
基本上还是比较全面的,一般的都够用了,但是还是有时候有的验证需要自定义。就以上面的代码为例,我们在添加TAG时需要检查系统之前是否已经存在这个TAG,如果存在则不让用户添加。这个就需要在添加之前去查询数据库,看该TAG是否已经存在,这里我们就需要自定一个验证规则了。
关键有一下两个步骤:
1、在rules中添加代码:array('tagname','checktagname','on'=>'create,update'),//插入TAG时检查是否已经存在该tag
注:我在其中用了'on'=>'create,update',所以这个验证规则之对create,update场景生效
2、在该模型(models)中添加验证函数:
publicfunctionchecktagname($attribute,$params){ $oldtag=Tag::model()->findByAttributes(array('tagname'=>$this->tagname)); if($oldtag->tagid>0){ $this->addError($attribute,'该TAG已经存在!'); } }
其中需要说明的是:
(1)该验证函数的参数必须是($attribute,$params),不能缺少其中任何一个;
(2)$this->addError($attribute,'该TAG已经存在!');这个是你想要在视图中输出的错误提示信息。
就是这么简单,有了这个方法,表单验证的各种想要的规则就都可以自定义了。
下面给大家介绍Yii自定义验证规则
最简单的定义验证规则的方法是在使用它的模型(model)内部定义。
比方说,你要检查用户的密码是否足够安全.
通常情况下你会使用CRegularExpression方法验证,但为了本指南,我们假设不存在此验证方法.
首先在模型(model)中添加两个常量
constWEAK=0;
constSTRONG=1;然后在模型(model)的rules方法中设置:
/** *@returnarrayvalidationrulesformodelattributes. */ publicfunctionrules() { returnarray( array('password','passwordStrength','strength'=>self::STRONG), ); }
确保你写的规则不是一个已经存在的规则,否则将会报错.
现在要做的是在模型(model)中创建一个名称为上面填写的规则的方法(即passwordStrength)。
/** *checkiftheuserpasswordisstrongenough *checkthepasswordagainstthepatternrequested *bythestrengthparameter *Thisisthe'passwordStrength'validatorasdeclaredinrules(). */ publicfunctionpasswordStrength($attribute,$params) { if($params['strength']===self::WEAK) $pattern='/^(?=.*[a-zA-Z0-9]).{5,}$/'; elseif($params['strength']===self::STRONG) $pattern='/^(?=.*\d(?=.*\d))(?=.*[a-zA-Z](?=.*[a-zA-Z])).{5,}$/'; if(!preg_match($pattern,$this->$attribute)) $this->addError($attribute,'yourpasswordisnotstrongenough!'); }
刚才创建的方法需要两个参数:*$attribute需要验证的属性*$params在规则中自定义的参数
在模型的rules方法中我们验证的是password属性,所以在验证规则中需要验证的属性值应该是password.
在rules方法中我们还设置了自定义的参数strength,它的值将会放到$params数组中.
你会发现在方法中我们使用了CModel::addError().
添加错误接受两个参数:第一个参数是在表单中显示错误的属性名,第二个参数时显示的错误信息。
完整的方法:继承CValidator类
如果你想把规则使用在多个模型(model)中,最好的方法时继承CValidator类。
继承这个类你可以使用像CActiveForm::$enableClientValidation(Yii1.1.7版本后可用)类似的其他功能。
创建类文件
首先要做的是创建类文件.最好的方法时类的文件名和类名相同,可以使用yii的延迟加载(lazyloading)功能。
让我们在应用(application)的扩展(extensiions)目录(在protected文件夹下)下新建一个文件夹.
将目录命名为:MyValidators
然后创建文件:passwordStrength.php
在文件中创建我们的验证方法
classpasswordStrengthextendsCValidator { public$strength; private$weak_pattern='/^(?=.*[a-zA-Z0-9]).{5,}$/'; private$strong_pattern='/^(?=.*\d(?=.*\d))(?=.*[a-zA-Z](?=.*[a-zA-Z])).{5,}$/'; ... }
在类中创建属性,此属性为在验证规则中使用的参数.
CValidator会自动根据参数来填充这些属性.
我们也创建了两个其他的属性,它们为preg_match函数使用的正则表达式.
现在我们应该重写父类的抽象方法(abstractmethod)validateAttribute
/** *Validatestheattributeoftheobject. *Ifthereisanyerror,theerrormessageisaddedtotheobject. *@paramCModel$objecttheobjectbeingvalidated *@paramstring$attributetheattributebeingvalidated */ protectedfunctionvalidateAttribute($object,$attribute) { //checkthestrengthparameterusedinthevalidationruleofourmodel if($this->strength=='weak') $pattern=$this->weak_pattern; elseif($this->strength=='strong') $pattern=$this->strong_pattern; //extracttheattributevaluefromit'smodelobject $value=$object->$attribute; if(!preg_match($pattern,$value)) { $this->addError($object,$attribute,'yourpasswordistooweak!'); } }
上面的方法我认为就不用解释了.当然你也可以在if的条件中使用常量,我推荐使用.