The rules part of YII models are validation rules for some forms, for forms Verification is very useful. If a form is added to the corresponding views, the program will automatically verify it in the rules before it is submitted. It can only be submitted after passing the valid restriction rules. It can be very effective. Ensure form security and information validity. Let me give you a detailed explanation:
The following is the simple code for the views part:
<?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'tag-form', 'enableAjaxValidation'=>false, )); ?> <div class="row"> <?php echo $form->labelEx($model,'tagname'); ?> <?php echo $form->textField($model,'tagname',array('size'=>20,'maxlength'=>32)); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'tagtype'); ?> <?php echo $form->radioButtonList($model,'tagtype'array(1=>"普通TAG",2=>"系统默认TAG"),array('separator'=>'','labelOptions'=>array('class'=>'tagtypelabel'))); ?> </div> <?php echo $form->errorSummary($model); ?> <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? '添加' : '修改'); ?> </div> <?php $this->endWidget(); ?>
Simple code for the rules part of models:
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'), ); }
The system has these verification rules by default:
boolean: Alias of CBooleanValidator, ensure the value of the property is CBooleanValidator::trueValue or CBooleanValidator::falseValue.
captcha : Alias of CCaptchaValidator, ensuring that the value of the attribute is equal to the verification code displayed by CAPTCHA.
compare : Alias for CCompareValidator, ensures that the value of a property is equal to another property or constant.
email : Alias for CEmailValidator, ensuring that the value of the attribute is a valid email address.
default : Alias of CDefaultValueValidator, assigns a default value to the attribute.
exist : Alias of CExistValidator, ensures that the attribute value exists in the specified data table field.
file : Alias for CFileValidator, ensuring that the attribute includes the name of an uploaded file.
filter: Alias of CFilterValidator, using a filter to transform attributes.
in : Alias for CRangeValidator, ensuring that the attribute appears in a predetermined list of values.
length: Alias of CStringValidator, ensures that the length of the attribute is within the specified range.
match : Alias of CRegularExpressionValidator, ensures that the attribute matches a regular expression.
numerical : Alias for CNumberValidator, ensuring that the attribute is a valid number.
required: Alias of CRequiredValidator, ensures that the attribute is not empty.
type : Alias of CTypeValidator, ensuring that the attribute is of the specified data type.
unique : Alias of CUniqueValidator, ensuring that attributes are unique among data table fields.
url : Alias for CUrlValidator, ensuring the attribute is a valid path.
Basically, it is relatively comprehensive, and the general ones are enough, but sometimes some verifications need to be customized. Taking the above code as an example, when adding a TAG, we need to check whether the TAG already exists in the system. If it exists, the user will not be allowed to add it. This requires querying the database before adding it to see if the TAG already exists. Here we need to customize a verification rule.
The key steps are as follows:
1. Add code in rules: array('tagname', 'checktagname', 'on'=>'create,update'), //When inserting TAG, check whether the tag already exists
Note: I used 'on'=>'create,update', so this verification rule takes effect on the create,update scenario
2. Add verification function in the models:
public function checktagname($attribute,$params){ $oldtag = Tag::model()->findByAttributes(array('tagname'=>$this->tagname)); if($oldtag->tagid > 0){ $this->addError($attribute, '该TAG已经存在!'); } }
What needs to be explained is:
(1) The parameters of the verification function must be ($attribute, $params), and any one of them cannot be missing;
(2)$this->addError($attribute, 'The TAG already exists!'); This is the error message you want to output in the view.
It’s that simple. With this method, all the desired rules for form validation can be customized.
The following introduces Yii custom verification rules
The simplest way to define a validation rule is to define it inside the model that uses it.
For example, you want to check whether the user’s password is secure enough.
Normally you would use the CRegularExpression method for validation, but for the sake of this guide we will assume that this validation method does not exist.
First add two constants in the model
const WEAK = 0;
const STRONG = 1; Then set it in the rules method of the model:
/** * @return array validation rules for model attributes. */ public function rules() { return array( array('password', 'passwordStrength', 'strength'=>self::STRONG), ); }
Make sure the rule you write is not an existing rule, otherwise an error will be reported.
What you need to do now is to create a method in the model with the name of the rule filled in above (i.e. 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 = '/^(?=.*[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, 'your password is not strong enough!'); }
The method just created requires two parameters: * $attribute The attribute that needs to be verified * $params The parameters customized in the rule
In the rules method of the model, we verify the password attribute, so the attribute value that needs to be verified in the verification rules should be password.
In the rules method we also set a custom parameter strength, and its value will be placed in the $params array.
You will find that in the method we use CModel::addError().
Add error accepts two parameters: the first parameter is the attribute name of the error displayed in the form, and the second parameter is the error message displayed.
Complete method: inherit the CValidator class
If you want to use rules in multiple models, the best way is to inherit the CValidator class.
继承这个类你可以使用像 CActiveForm::$enableClientValidation (Yii 1.1.7 版本后可用) 类似的其他功能。
创建类文件
首先要做的是创建类文件.最好的方法时类的文件名和类名相同,可以使用 yii 的延迟加载(lazy loading)功能。
让我们在应用(application)的扩展(extensiions)目录(在 protected 文件夹下)下新建一个文件夹.
将目录命名为: MyValidators
然后创建文件: passwordStrength.php
在文件中创建我们的验证方法
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,}$/'; ... }
在类中创建属性,此属性为在验证规则中使用的参数.
CValidator 会自动根据参数来填充这些属性.
我们也创建了两个其他的属性,它们为 preg_match 函数使用的正则表达式.
现在我们应该重写父类的抽象方法(abstract method) validateAttribute
/** * 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 的条件中使用常量,我推荐使用.