日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Yii自定义验证规则

發(fā)布時(shí)間:2025/6/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Yii自定义验证规则 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

簡(jiǎn)單的方法:在 model 內(nèi)部定義規(guī)則

最簡(jiǎn)單的定義驗(yàn)證規(guī)則的方法是在使用它的模型(model)內(nèi)部定義。

比方說(shuō),你要檢查用戶(hù)的密碼是否足夠安全.

通常情況下你會(huì)使用 CRegularExpression 方法驗(yàn)證,但為了本指南,我們假設(shè)不存在此驗(yàn)證方法.

首先在模型(model)中添加兩個(gè)常量

const WEAK = 0; const STRONG = 1;

然后在模型(model)的 rules 方法中設(shè)置:

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

確保你寫(xiě)的規(guī)則不是一個(gè)已經(jīng)存在的規(guī)則,否則將會(huì)報(bào)錯(cuò).

現(xiàn)在要做的是在模型(model)中創(chuàng)建一個(gè)名稱(chēng)為上面填寫(xiě)的規(guī)則的方法(即 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!'); }

剛才創(chuàng)建的方法需要兩個(gè)參數(shù):* $attribute 需要驗(yàn)證的屬性* $params 在規(guī)則中自定義的參數(shù)

在模型的 rules 方法中我們驗(yàn)證的是 password 屬性,所以在驗(yàn)證規(guī)則中需要驗(yàn)證的屬性值應(yīng)該是?password.

在 rules 方法中我們還設(shè)置了自定義的參數(shù) strength,它的值將會(huì)放到 $params 數(shù)組中.

你會(huì)發(fā)現(xiàn)在方法中我們使用了 CModel::addError().

添加錯(cuò)誤接受兩個(gè)參數(shù):第一個(gè)參數(shù)是在表單中顯示錯(cuò)誤的屬性名,第二個(gè)參數(shù)時(shí)顯示的錯(cuò)誤信息 。

?

完整的方法:繼承 CValidator 類(lèi)

如果你想把規(guī)則使用在多個(gè)模型(model)中,最好的方法時(shí)繼承 CValidator 類(lèi)。

繼承這個(gè)類(lèi)你可以使用像 CActiveForm::$enableClientValidation (Yii 1.1.7 版本后可用) 類(lèi)似的其他功能。

創(chuàng)建類(lèi)文件

首先要做的是創(chuàng)建類(lèi)文件.最好的方法時(shí)類(lèi)的文件名和類(lèi)名相同,可以使用 yii 的延遲加載(lazy loading)功能。

讓我們?cè)趹?yīng)用(application)的擴(kuò)展(extensiions)目錄(在 protected 文件夾下)下新建一個(gè)文件夾.

將目錄命名為:?MyValidators

然后創(chuàng)建文件:?passwordStrength.php

在文件中創(chuàng)建我們的驗(yàn)證方法

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

在類(lèi)中創(chuàng)建屬性,此屬性為在驗(yàn)證規(guī)則中使用的參數(shù).

CValidator 會(huì)自動(dòng)根據(jù)參數(shù)來(lái)填充這些屬性.

我們也創(chuàng)建了兩個(gè)其他的屬性,它們?yōu)?preg_match 函數(shù)使用的正則表達(dá)式.

現(xiàn)在我們應(yīng)該重寫(xiě)父類(lèi)的抽象方法(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!'); } }

上面的方法我認(rèn)為就不用解釋了.當(dāng)然你也可以在 if 的條件中使用常量,我推薦使用.

實(shí)現(xiàn)客戶(hù)端驗(yàn)證

如果要實(shí)現(xiàn)客戶(hù)端驗(yàn)證還需要重寫(xiě)類(lèi)中的方法 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!')."); } "; }

正如你看到的此方法簡(jiǎn)單的返回了一個(gè)在驗(yàn)證中將使用到的 javascript.

最后一步:在模塊(model)中怎么使用自定義的驗(yàn)證類(lèi)

下面有幾種方法來(lái)實(shí)現(xiàn):

你可以在返回 規(guī)則數(shù)組(ruels array)前 使用 Yii::import 方法,或使用Yii的符號(hào)方式:

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

參考:
HDR

轉(zhuǎn)載于:https://www.cnblogs.com/DaBing0806/p/4744118.html

總結(jié)

以上是生活随笔為你收集整理的Yii自定义验证规则的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。