您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何理解PHP YII框架開發(fā)中模型中rules自定義驗(yàn)證規(guī)則”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
以下是視圖(views)部分的簡(jiǎn)單代碼:
<?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'tag-form', 'enableAjaxValidation'=>false, )); ?> <div class="row"> <?php echo $form->labelEx($model,'tagname'); ?> <?php echo $form->textField($model,'tagname',array('size'=>20,'maxlength'=>32)); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'tagtype'); ?> <?php echo $form->radioButtonList($model,'tagtype'array(1=>"普通TAG",2=>"系統(tǒng)默認(rèn)TAG"),array('separator'=>'','labelOptions'=>array('class'=>'tagtypelabel'))); ?> </div> <?php echo $form->errorSummary($model); ?> <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? '添加' : '修改'); ?> </div> <?php $this->endWidget(); ?>
模型(models)中rules部分的簡(jiǎn)單代碼:
public function rules() { return array( array('tagname,tagtype', 'required'), array('tagtype', 'numerical', 'integerOnly'=>true), array('tagname', 'length', 'max'=>32), array('tagname', 'match', 'pattern'=>'/^[\x{4e00}-\x{9fa5}A-Za-z0-9]+$/u', 'message'=>'標(biāo)簽不合法,必須為漢字、字母或者數(shù)字!'), array('tagname', 'checktagname', 'on'=>'create,update'),//插入TAG時(shí)檢查是否已經(jīng)存在該tag array('tagid, tagname, tagtype', 'safe', 'on'=>'search'), ); }
系統(tǒng)默認(rèn)有這些驗(yàn)證規(guī)則:
boolean : CBooleanValidator 的別名, 確保屬性的值是CBooleanValidator::trueValue 或 CBooleanValidator::falseValue .
captcha : CCaptchaValidator 的別名,確保了特性的值等于 CAPTCHA 顯示出來(lái)的驗(yàn)證碼.
compare : CCompareValidator 的別名, 確保了特性的值等于另一個(gè)特性或常量.
email : CEmailValidator 的別名,確保了特性的值是一個(gè)有效的電郵地址.
default : CDefaultValueValidator 的別名, 為特性指派了一個(gè)默認(rèn)值.
exist : CExistValidator 的別名, 確保屬性值存在于指定的數(shù)據(jù)表字段中.
file : CFileValidator 的別名, 確保了特性包含了一個(gè)上傳文件的名稱.
filter : CFilterValidator 的別名, 使用一個(gè)filter轉(zhuǎn)換屬性.
in : CRangeValidator 的別名, 確保了特性出現(xiàn)在一個(gè)預(yù)訂的值列表里.
length : CStringValidator 的別名, 確保了特性的長(zhǎng)度在指定的范圍內(nèi).
match : CRegularExpressionValidator 的別名, 確保了特性匹配一個(gè)正則表達(dá)式.
numerical : CNumberValidator 的別名, 確保了特性是一個(gè)有效的數(shù)字.
required : CRequiredValidator 的別名, 確保了特性不為空.
type : CTypeValidator 的別名, 確保了特性為指定的數(shù)據(jù)類型.
unique : CUniqueValidator 的別名, 確保了特性在數(shù)據(jù)表字段中是唯一的.
url : CUrlValidator 的別名, 確保了特性是一個(gè)有效的路徑.
基本上還是比較全面的,一般的都?jí)蛴昧?,但是還是有時(shí)候有的驗(yàn)證需要自定義。就以上面的代碼為例,我們?cè)谔砑覶AG時(shí)需要檢查系統(tǒng)之前是否已經(jīng)存在這個(gè)TAG,如果存在則不讓用戶添加。這個(gè)就需要在添加之前去查詢數(shù)據(jù)庫(kù),看該TAG是否已經(jīng)存在,這里我們就需要自定一個(gè)驗(yàn)證規(guī)則了。
關(guān)鍵有一下兩個(gè)步驟:
1、在rules中 添加代碼:array('tagname', 'checktagname', 'on'=>'create,update'),//插入TAG時(shí)檢查是否已經(jīng)存在該tag
注:我在其中用了 'on'=>'create,update',所以這個(gè)驗(yàn)證規(guī)則之對(duì)create,update場(chǎng)景生效
2、在該模型(models)中添加驗(yàn)證函數(shù):
public function checktagname($attribute,$params){ $oldtag = Tag::model()->findByAttributes(array('tagname'=>$this->tagname)); if($oldtag->tagid > 0){ $this->addError($attribute, '該TAG已經(jīng)存在!'); } }
其中需要說(shuō)明的是:
(1)該驗(yàn)證函數(shù)的參數(shù)必須是($attribute,$params),不能缺少其中任何一個(gè);
(2)$this->addError($attribute, '該TAG已經(jīng)存在!');這個(gè)是你想要在視圖中輸出的錯(cuò)誤提示信息。
就是這么簡(jiǎn)單,有了這個(gè)方法,表單驗(yàn)證的各種想要的規(guī)則就都可以自定義了。
下面給大家介紹Yii自定義驗(yàn)證規(guī)則
最簡(jiǎn)單的定義驗(yàn)證規(guī)則的方法是在使用它的模型(model)內(nèi)部定義。
比方說(shuō),你要檢查用戶的密碼是否足夠安全.
通常情況下你會(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), ); }
確保你寫的規(guī)則不是一個(gè)已經(jīng)存在的規(guī)則,否則將會(huì)報(bào)錯(cuò).
現(xiàn)在要做的是在模型(model)中創(chuàng)建一個(gè)名稱為上面填寫的規(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 類
如果你想把規(guī)則使用在多個(gè)模型(model)中,最好的方法時(shí)繼承 CValidator 類。
繼承這個(gè)類你可以使用像 CActiveForm::$enableClientValidation (Yii 1.1.7 版本后可用) 類似的其他功能。
創(chuàng)建類文件
首先要做的是創(chuàng)建類文件.最好的方法時(shí)類的文件名和類名相同,可以使用 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,}$/'; ... }
在類中創(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)該重寫父類的抽象方法(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!'); } }
“如何理解PHP YII框架開發(fā)中模型中rules自定義驗(yàn)證規(guī)則”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。