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

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

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) 評論(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>
            免费成人av在线| 欧美日韩在线视频一区| 麻豆国产精品va在线观看不卡| 欧美激情bt| 亚洲大片在线观看| 久久久久久久久久久久久久一区| 亚洲日本一区二区| 免费成人高清视频| 极品日韩久久| 亚洲欧美在线播放| 久久一区二区精品| 久久久精品欧美丰满| 久久亚洲高清| 国产亚洲欧美aaaa| 亚洲欧美一区二区在线观看| 一本色道婷婷久久欧美| 欧美噜噜久久久xxx| 最新高清无码专区| 亚洲国产天堂久久综合网| 狼狼综合久久久久综合网| 在线精品亚洲一区二区| 免费亚洲一区| 欧美va亚洲va香蕉在线| 亚洲免费久久| 亚洲色图综合久久| 国产乱码精品一区二区三区五月婷| 亚洲在线一区| 羞羞视频在线观看欧美| 国产一区二区三区在线观看免费 | 欧美成人精品不卡视频在线观看| 久久精品国亚洲| 亚洲第一毛片| 亚洲欧洲精品一区二区精品久久久| 欧美精品性视频| 午夜精品在线| 久久久精彩视频| 亚洲精品国精品久久99热| 亚洲美女网站| 国产亚洲欧美一区| 欧美激情视频一区二区三区在线播放 | 香蕉乱码成人久久天堂爱免费| 国产一区二区中文| 欧美成人免费在线视频| 欧美日韩日本视频| 久久久999精品视频| 欧美jizzhd精品欧美巨大免费| 亚洲天堂av在线免费| 午夜一级久久| 日韩系列欧美系列| 欧美一区二区精品久久911| 在线看一区二区| av成人黄色| 在线欧美日韩| 亚洲性人人天天夜夜摸| 亚洲高清资源| 亚洲伊人伊色伊影伊综合网| 1000部国产精品成人观看| 一区二区三区免费在线观看| 在线欧美不卡| 久久电影一区| 亚洲一区二区高清| 欧美a级理论片| 久久久久久69| 国产精品嫩草久久久久| 亚洲欧美视频在线观看| 欧美一区二区三区播放老司机| 麻豆久久婷婷| 久久成人免费日本黄色| 欧美日本精品| 欧美777四色影视在线| 国产精品日韩欧美大师| 亚洲三级国产| 亚洲精品在线观| 久久九九热免费视频| 欧美亚洲自偷自偷| 欧美色区777第一页| 亚洲欧洲精品一区二区三区不卡| 黑丝一区二区三区| 欧美在线|欧美| 午夜精品视频网站| 欧美性事免费在线观看| 亚洲日本中文字幕免费在线不卡| 在线精品视频免费观看| 久久久午夜电影| 美女日韩欧美| 亚洲二区免费| 蜜桃av一区二区| 欧美高清视频在线| 亚洲国产欧美精品| 免费在线亚洲| 亚洲三级视频| 亚洲一区久久久| 欧美婷婷久久| 宅男噜噜噜66一区二区| 亚洲一区二区三区四区五区午夜 | 亚洲欧美韩国| 久久av一区| 国产精品视频yy9099| 亚洲一区二区毛片| 午夜精品久久久久久久99黑人| 国产精品盗摄一区二区三区| 一区二区动漫| 香蕉av福利精品导航| 国产欧美韩国高清| 欧美一级专区| 你懂的成人av| 亚洲激情一区二区三区| 欧美成人自拍| 99精品视频一区| 午夜视黄欧洲亚洲| 国产一区二区三区日韩欧美| 久久婷婷影院| 最近中文字幕日韩精品| 亚洲视频精选在线| 国产欧美日本| 裸体女人亚洲精品一区| 亚洲激情专区| 亚洲欧美中文字幕| 精品成人国产在线观看男人呻吟| 美女露胸一区二区三区| 亚洲精品资源| 欧美在线观看一二区| 亚洲第一中文字幕| 欧美日韩国产首页在线观看| 一区二区三区四区精品| 久久亚洲综合网| 亚洲午夜小视频| 狠狠操狠狠色综合网| 欧美精品久久99久久在免费线| 亚洲制服av| 久久精品伊人| 欧美精品不卡| 欧美亚洲免费| 亚洲精品乱码久久久久久蜜桃麻豆| 销魂美女一区二区三区视频在线| 激情偷拍久久| 国产精品高潮久久| 久热爱精品视频线路一| 亚洲深夜福利| 亚洲国产va精品久久久不卡综合| 亚洲一区二区三区色| 亚洲国产精品专区久久| 国产美女在线精品免费观看| 欧美成人午夜影院| 欧美在线视频免费观看| 99天天综合性| 亚洲福利视频专区| 久久亚洲一区二区三区四区| 亚洲一区二区三区在线观看视频| 尤物yw午夜国产精品视频| 国产精品久久久久9999高清| 欧美+亚洲+精品+三区| 久久久精品五月天| 性xx色xx综合久久久xx| 一区二区三区欧美视频| 亚洲激情在线观看| 欧美激情精品| 欧美h视频在线| 久久久www成人免费无遮挡大片 | 久久精品五月| 欧美在线国产| 亚洲制服丝袜在线| 日韩性生活视频| 91久久久久久久久| 在线免费一区三区| 在线观看av不卡| 极品尤物av久久免费看| 国产婷婷97碰碰久久人人蜜臀| 国产精品久久久久91| 欧美午夜精品理论片a级按摩| 亚洲国产精品久久久久秋霞蜜臀| 噜噜噜在线观看免费视频日韩 | 久久久夜精品| 午夜亚洲视频| 欧美资源在线观看| 久久av一区二区| 欧美一区二区播放| 中文精品在线| 亚洲一区在线观看免费观看电影高清| 夜夜嗨av一区二区三区四区| 日韩视频免费大全中文字幕| 99精品欧美一区| 国产精品99久久99久久久二8| 日韩亚洲在线观看| 在线视频你懂得一区| 亚洲欧美电影在线观看| 性欧美xxxx大乳国产app| 午夜亚洲视频| 久久成人一区二区| 久久一区中文字幕| 欧美激情在线免费观看| 欧美激情一区二区三级高清视频| 亚洲国产小视频| 一区二区三区四区五区视频| 亚洲制服少妇| 久久人人爽爽爽人久久久| 欧美暴力喷水在线| 欧美体内she精视频| 国产一区二区精品久久99| 亚洲国产精品成人精品|