青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

xiaoguozi's Blog
Pay it forword - 我并不覺的自豪,我所嘗試的事情都失敗了······習(xí)慣原本生活的人不容易改變,就算現(xiàn)狀很糟,他們也很難改變,在過程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛傳出去,很困難,也無法預(yù)料,人們需要更細(xì)心的觀察別人,要隨時(shí)注意才能保護(hù)別人,因?yàn)樗麄兾幢刂雷约阂裁础ぁぁぁぁ?/span>

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 小果子 閱讀(518) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 框架
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美性大战久久久久| 午夜精品久久久久久99热软件| 久久网站热最新地址| 亚洲国产日韩欧美一区二区三区| 影音先锋国产精品| 国产一区自拍视频| 国产欧美欧洲在线观看| 国产精品久久久久久久免费软件| 午夜激情亚洲| 欧美在线观看你懂的| 亚洲精品123区| 亚洲国产精品va在看黑人| 亚洲伊人第一页| 亚洲在线视频观看| 欧美午夜片在线观看| 亚洲黄色毛片| 亚洲美女在线国产| 欧美日韩一区在线| 久久综合九色综合欧美狠狠| 久久伊人亚洲| 女主播福利一区| 欧美日韩精选| 国产情人综合久久777777| 韩国欧美一区| 99www免费人成精品| 亚洲一区二区三区四区在线观看| 欧美一区二区高清在线观看| 欧美成人精品在线视频| 一区二区三区蜜桃网| 久久久国产精彩视频美女艺术照福利 | 欧美亚洲三区| 欧美日韩国产欧美日美国产精品| 国产亚洲精品久久久| 亚洲精品一二区| 久久久久久久久久久成人| 欧美成人69av| 小嫩嫩精品导航| 欧美日韩在线视频观看| 在线观看国产成人av片| 在线性视频日韩欧美| 久久福利资源站| 最新国产拍偷乱拍精品| 欧美在线观看你懂的| 国产精品www色诱视频| 娇妻被交换粗又大又硬视频欧美| 99热这里只有成人精品国产| 一区二区三区国产精华| 欧美一区二区| 亚洲无人区一区| 亚洲国产一区二区a毛片| 免费在线成人av| 欧美在线你懂的| 久久伊人免费视频| 免费在线欧美黄色| 最新亚洲一区| 中文精品在线| 亚洲制服av| 久久精品欧美日韩精品| 久久婷婷丁香| 久久精品视频免费观看| 亚洲欧美综合国产精品一区| 欧美一区二区在线看| 久久嫩草精品久久久久| 久久精品中文字幕一区二区三区| 久久婷婷丁香| 亚洲第一网站| 一本色道88久久加勒比精品| 亚洲一区二区在线免费观看视频 | 亚洲国产一区二区精品专区| 妖精视频成人观看www| 午夜伦欧美伦电影理论片| 久久精品视频免费观看| 欧美激情片在线观看| 国产精品第十页| 欧美大香线蕉线伊人久久国产精品| 亚洲电影av| 久久婷婷麻豆| 亚洲国产成人在线| 亚洲永久免费精品| 牛牛国产精品| 国产日韩精品久久| 日韩一级免费观看| 老司机免费视频一区二区三区| 亚洲国产一二三| 亚洲一区二区在线看| 久久综合福利| 国产视频一区二区三区在线观看| 亚洲精品久久久久中文字幕欢迎你| 午夜精品久久久久久久| 欧美在线视频一区| 最新日韩精品| 久久综合综合久久综合| 国产精品日韩欧美一区二区| 亚洲另类视频| 久久蜜桃精品| 亚洲欧美成人一区二区在线电影| 毛片一区二区| 国产一区二区三区的电影| 亚洲午夜影视影院在线观看| 亚洲第一黄网| 美女精品视频一区| 韩国成人精品a∨在线观看| 亚洲一区二区3| 亚洲一区二区三区久久| 亚洲人体一区| 欧美乱大交xxxxx| 亚洲精品免费在线| 欧美高清自拍一区| 夜夜精品视频一区二区| 美女被久久久| 影音先锋一区| 久久久不卡网国产精品一区| 午夜国产一区| 狠狠爱综合网| 免费看黄裸体一级大秀欧美| 欧美寡妇偷汉性猛交| 国产日韩一区二区| 久久精品30| 久久久www成人免费精品| 国产一区二区成人久久免费影院| 亚洲一级黄色| 亚洲国产欧美一区二区三区久久 | 一本在线高清不卡dvd | 亚洲自拍啪啪| 欧美性感一类影片在线播放| 亚洲国产精品高清久久久| 欧美大片在线观看| 欧美在线免费视频| 久久精品国产91精品亚洲| 亚洲一区二区三区影院| 亚洲午夜一区| 亚洲制服欧美中文字幕中文字幕| 亚洲制服少妇| 亚洲免费久久| 亚洲一区二区免费视频| 亚洲精品乱码久久久久久日本蜜臀| 91久久久久久| 亚洲精品永久免费| 亚洲影音一区| 久久九九国产| 欧美电影免费观看| 亚洲三级影片| 欧美理论电影网| 欧美激情一区二区三区高清视频 | 欧美激情aaaa| 亚洲一级电影| 国产精品swag| 牛人盗摄一区二区三区视频| 黄色亚洲大片免费在线观看| 欧美sm重口味系列视频在线观看| 国产一区二区精品丝袜| 久久综合激情| 欧美国产日韩一区| 91久久午夜| 国产精品―色哟哟| 久久久亚洲高清| 欧美日韩aaaaa| 欧美在线电影| 欧美国产欧美亚州国产日韩mv天天看完整 | 国产亚洲欧美一区| 亚洲国产精品久久久久久女王| 国产精品呻吟| 亚洲国产成人精品久久| 久久一区欧美| 亚洲欧美色婷婷| 欧美电影免费网站| 欧美伊人精品成人久久综合97| 欧美 日韩 国产一区二区在线视频| 欧美一区二区三区视频免费| 久久天堂国产精品| 校园春色国产精品| 欧美成人日韩| 黄色综合网站| 欧美国产国产综合| 免费亚洲婷婷| 激情综合色丁香一区二区| 中国日韩欧美久久久久久久久| 亚洲电影av| 欧美制服丝袜| 久久久噜噜噜久久| 久久久五月天| 久久激情视频| 国内激情久久| 欧美在线短视频| 久久久久这里只有精品| 国产亚洲午夜高清国产拍精品| 亚洲专区免费| 国内外成人免费激情在线视频| 欧美日本一道本| 亚洲理论电影网| 日韩亚洲欧美一区| 欧美精品久久久久久| 嫩模写真一区二区三区三州| 国产一区二区三区观看| 亚洲自拍都市欧美小说| 欧美在线高清| 国内精品亚洲| 国产一区二区三区观看| 久久精品亚洲精品| 欧美v日韩v国产v|