YII的models中的rules部分是一些表单的验证规则,对于表单验证十分有用,在相应的视图(views)里面添加了表单,在表单被提交之前程序都会自动先来这里面的规则里验证,只有通过对其有效的限制规则后才能被提交,可以很有效地保证表单安全和信息的有效性。还是给大家具体说明一下:
以下是视图(views)部分的简单代码:
<"row"> <"row"> <"普通TAG",2=>"系统默认TAG"),array('separator'=>'','labelOptions'=>array('class'=>'tagtypelabel'))); "row buttons"> <"htmlcode">public function rules() { return array( 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)中添加验证函数:
public function checktagname($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)中添加两个常量
const WEAK = 0;
const STRONG = 1;然后在模型(model)的 rules 方法中设置:/** * @return array validation rules for model attributes. */ public function rules() { return array( array('password', 'passwordStrength', 'strength'=>self::STRONG), ); }确保你写的规则不是一个已经存在的规则,否则将会报错.
现在要做的是在模型(model)中创建一个名称为上面填写的规则的方法(即 passwordStrength)。
/** * check if the user password is strong enough * check the password against the pattern requested * by the strength parameter * This is the 'passwordStrength' validator as declared in rules(). */ public function passwordStrength($attribute,$params) { if ($params['strength'] === self::WEAK) $pattern = '/^("htmlcode">class passwordStrength extends CValidator { public $strength; private $weak_pattern = '/^("htmlcode">/** * 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!'); } }上面的方法我认为就不用解释了.当然你也可以在 if 的条件中使用常量,我推荐使用.
华山资源网 Design By www.eoogi.com
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】