Yii custom verification rules simple method: define rules within the model
The simplest way to define a verification rule is to define it within the model that uses it.
For example, you need to check whether your password is safe enough.
In general, you will use the CRegularExpression method for verification, but for this guide, we assume that this verification method does not exist.
First, add two constants to the model.
12 |
ConstWEAK = 0; constSTRONG = 1; |
Then, in the model's rules method, set:
123456789 |
/*** @ Return array validation rules for model attributes. */publicfunctionrules () {returnarray (array ('passwordstrength ', 'strength' => self: STRONG ),);} |
Make sure that the rule you write is not an existing rule. Otherwise, an error will be reported.
Now, you need to create a method (passwordStrength) named as the rule entered above in the model ).
12345678910111213141516 |
/*** 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 (). */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, 'Your password is not strong enough! ');} |
The method just created requires two parameters: * $ attribute to be verified * $ params custom parameters in the rule
In the model's rules method, we verify the password attribute, so the attribute value to be verified in the validation rule should bePassword.
In the rules method, we also set the custom parameter strength. its value will be placed in the $ params array.
You will find that CModel: addError () is used in the method ().
Two parameters are accepted for adding an error: The first parameter displays the incorrect attribute name in the form and the second parameter displays the error message.
Complete Method: inherit the CValidator class
If you want to use rules in multiple models, the best method inherits the CValidator class.
To inherit from this class, you can use other functions similar to CActiveForm: $ enableClientValidation (available after Yii 1.1.7.
Create a class file
The first thing to do is to create a class file. when the best method is, the class file name is the same as the class name. you can use the lazy loading function of yii.
Let's create a folder under the extensiions Directory of the application (under the protected folder.
Name the directory as follows:MyValidators
Then create the file:PasswordStrength. php
Create our verification method in the file
123456789 |
ClasspasswordStrengthextendsCValidator {public $ strength; private $ weak_pattern = '/^ (? =. * [A-zA-Z0-9]). {5,} $/'; private $ strong_pattern ='/^ (? =. * \ D (? =. * \ D ))(? =. * [A-zA-Z] (? =. * [A-zA-Z]). {5,} $ /';...} |
Create an attribute in the class, which is a parameter used in the validation rule.
CValidator automatically fills in these attributes based on parameters.
We have also created two other attributes, which are the regular expressions used by the preg_match function.
Now we should rewrite the abstract method of the parent class (validateAttribute ).