日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Yii自定义验证规则

發布時間:2025/6/17 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Yii自定义验证规则 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單的方法:在 model 內部定義規則

最簡單的定義驗證規則的方法是在使用它的模型(model)內部定義。

比方說,你要檢查用戶的密碼是否足夠安全.

通常情況下你會使用 CRegularExpression 方法驗證,但為了本指南,我們假設不存在此驗證方法.

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

const WEAK = 0; const STRONG = 1;

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

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

確保你寫的規則不是一個已經存在的規則,否則將會報錯.

現在要做的是在模型(model)中創建一個名稱為上面填寫的規則的方法(即 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!'); }

剛才創建的方法需要兩個參數:* $attribute 需要驗證的屬性* $params 在規則中自定義的參數

在模型的 rules 方法中我們驗證的是 password 屬性,所以在驗證規則中需要驗證的屬性值應該是?password.

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

你會發現在方法中我們使用了 CModel::addError().

添加錯誤接受兩個參數:第一個參數是在表單中顯示錯誤的屬性名,第二個參數時顯示的錯誤信息 。

?

完整的方法:繼承 CValidator 類

如果你想把規則使用在多個模型(model)中,最好的方法時繼承 CValidator 類。

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

創建類文件

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

讓我們在應用(application)的擴展(extensiions)目錄(在 protected 文件夾下)下新建一個文件夾.

將目錄命名為:?MyValidators

然后創建文件:?passwordStrength.php

在文件中創建我們的驗證方法

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

在類中創建屬性,此屬性為在驗證規則中使用的參數.

CValidator 會自動根據參數來填充這些屬性.

我們也創建了兩個其他的屬性,它們為 preg_match 函數使用的正則表達式.

現在我們應該重寫父類的抽象方法(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!'); } }

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

實現客戶端驗證

如果要實現客戶端驗證還需要重寫類中的方法 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!')."); } "; }

正如你看到的此方法簡單的返回了一個在驗證中將使用到的 javascript.

最后一步:在模塊(model)中怎么使用自定義的驗證類

下面有幾種方法來實現:

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

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

參考:
HDR

轉載于:https://www.cnblogs.com/DaBing0806/p/4744118.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。