• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            xiaoguozi's Blog
            Pay it forword - 我并不覺的自豪,我所嘗試的事情都失敗了······習慣原本生活的人不容易改變,就算現(xiàn)狀很糟,他們也很難改變,在過程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛傳出去,很困難,也無法預(yù)料,人們需要更細心的觀察別人,要隨時注意才能保護別人,因為他們未必知道自己要什么·····

            Some times the core validation rules provided by Yii won't satisfy all your needs, so you'll need to create your very own validation rule.

            Easy approach: inside-model rule

            The easiest way to create a new validation rule is inside the model that is going to use it.

            Let's say that you want to check if a user password is safe enough.
            Usually you could achieve this result just by using the CRegularExpressionValidator but for the sake of this guide let's pretend that validator does not exist.

            first of all in your model class you'll have to add two constants

            const WEAK = 0; const STRONG = 1;

            then in your rules method you'll have to set the rule

            /**  * @return array validation rules for model attributes.  */ public function rules() {     return array(        array('password', 'passwordStrength', 'strength'=>self::STRONG),     ); }

            make sure that you won't give the rule the name of an existing one, otherwise you are going to have some troubles later.

            Now the only thing you need to do is create a new method inside the model, named after the validation rule you just declared.

            /**  * 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 new method you just created accepts two arguments:

            • $attribute = is the name of the attribute that the method is validating
            • $params = additional parameters that you could define in the rules

            In our rules method we used this rule on the password attribute, so the value of attribute inside our validation model will be password

            In the rule we also setted an additional parameter named strength
            the value of that parameter will be inside the $params array

            As you can see inside the method we are making a call to CModel::addError().
            Add Error accepts two parameters: the first one is the name of the attribute that you want to display the error in your form, the second one is the actual error string you want to be displayed.

            Complete approach: extending the CValidator class

            If you need your custom validation rule in more then one model the best thing to do is extending the CValidator class.
            Extending this class you also can take advantage of other features, like CActiveForm::$enableClientValidation, first implemented with Yii 1.1.7 release.

            Creating the class file

            The first thing that you have to do is create your class file. The best thing is to always name it after your class name, to best use Yii lazy loading feature. Let's create a new directory inside your application extensions directory (which is located inside the protected directory).
            Name this directory MyValidators.
            Then we create our own file: passwordStrength.php

            Inside this file create our CValidator class

            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,}$/'; ...

            In the class file create one attribute for each additional parameter that you want to use inside your validation rule.
            CValidator will take care to populate that attribute with the parameter value all by itself.
            We also created two other attributes, each containing the patterns we want to use in our preg_match function.

            Now we have to override the parent 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!');     } }

            The method above is self explanatory i think.
            Of course you could use constants in those IF, and I actually recommend it.

            Implementing Client Validation

            If you want to implement client validation you'll need to override another method inside your class: clientValidateAttribute

            /**  * 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!')."); } "; }

            As you can see this method simply returns the javascript that you need to use for your validation

            Last step: how to use your validation class inside the module rules

            There are several approach you can use here.

            You could first use Yii::import in the rules method before returning the rules array, or you can just use Yii dot notation:

            /**  * @return array validation rules for model attributes.  */ public function rules() {     return array(        array('password', 'ext.MyValidators.passwordStrength', 'strength'=>self::STRONG),     ); }
            more:
            http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/
            posted on 2012-12-26 15:39 小果子 閱讀(508) 評論(0)  編輯 收藏 引用 所屬分類: 框架
            97久久精品无码一区二区| 久久久久亚洲精品中文字幕| 久久人人爽爽爽人久久久| 热re99久久精品国99热| 狠狠色丁香久久综合五月| 97久久天天综合色天天综合色hd | 久久久久久极精品久久久| 香蕉久久AⅤ一区二区三区| 漂亮人妻被黑人久久精品| 伊人久久大香线蕉精品| 久久WWW免费人成一看片| 亚洲一区二区三区日本久久九| 四虎国产精品成人免费久久| 久久久噜噜噜www成人网| 久久亚洲国产精品五月天婷| 久久亚洲AV成人出白浆无码国产| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 国产精品久久99| 亚洲精品WWW久久久久久| 99久久无色码中文字幕人妻| 国产日韩欧美久久| 99久久久国产精品免费无卡顿 | 久久99精品久久久久久秒播| 久久精品麻豆日日躁夜夜躁| 伊人色综合久久天天人守人婷| 精品久久久久久国产91| 亚洲欧美国产精品专区久久| 中文字幕成人精品久久不卡 | 无码乱码观看精品久久| 国产成人综合久久精品尤物| 丁香五月网久久综合| 久久久久久人妻无码| 久久久久亚洲精品天堂| 日产精品久久久一区二区| 久久精品极品盛宴观看| 亚洲精品成人网久久久久久| 久久九九免费高清视频| 国产精品免费看久久久香蕉| 中文字幕亚洲综合久久2| 久久亚洲综合色一区二区三区| 久久婷婷国产麻豆91天堂|