2015-04-12 42 views
1

我已經定義了自定義的驗證規則HTML內容:Yii2呈現自定義的驗證規則addError方法

['Email', 'checkUniqueExistence'], 

public function checkUniqueExistence($attribute, $params) 
{ 
     $email = $this->$attribute; 

     $checkUniqueExistenceForEmail = Parents::find()->where([ 'Email' => $email])->One(); 

     $resendActivationLink = Html::a('Resend Link','#'); 

     if(count($checkUniqueExistenceForEmail) > 0){ 
      if($checkUniqueExistenceForEmail->IsAccountActivated == Yii::$app->params['IsAccountActivatedTrue']){ 
       $this->addError($attribute, 'Email address already exists'); 
      } 
      else{ 
       $this->addError($attribute, 'You haven\'t activated your account. Request '.$resendActivationLink); 
      } 
     } 

} 

但鏈接不會生成。它顯示爲一個字符串。提交表單後我得到以下輸出:

You haven't activated your account. Request <a href="#">Resend Link</a> 

是否有可能在addError()方法中顯示鏈接?

+0

更有效的方法將是有條件地基於誤差對象是否被設置或不.. –

+0

例如以顯示重新發送鏈接:如果($這 - > hasError ){show_link} else {hide_link},你可以在你的視圖中添加這樣的內容 –

+0

謝謝我會試試看。但問題仍然出現,爲什麼addError方法不呈現HTML? – Chinmay

回答

7

傳遞該配置數組ActiveField

'errorOptions' => ['encode' => false]