2012-06-25 40 views
2

我需要在我的應用程序的所有頁面上使用「Report Bug」模式表單。所以我以小部件的形式製作它,並將它放在我的應用程序的主佈局模板上。Yii CActiveForm客戶端驗證無法在我的Widget中工作

<? $reportBugForm = $this->widget('application.widgets.reportBugWidget.reportBug',array(),true); ?> 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     .... 
    </head> 
    <body> 
     <? echo $reportBugForm; ?> 

我把它放在頁面的頂部,因爲它應該張貼AJAX請求本身,而不考慮當前的頁面(我不希望使用任何控制器)。這裏是小部件類本身的代碼:

Yii::import('application.widgets.reportBugWidget.reportBugForm'); 

class reportBug extends CWidget { 

    public $id = 'reportBug'; 
    public $name = 'reportBugForm'; 
    public $form; 

    public function init() { 

     $this->form = new reportBugForm('error'); 

     if(Yii::app()->request->isAjaxRequest && isset($_POST[$this->name]) && !empty($_POST[$this->name])) { 
      $this->form->attributes = $_POST[$this->name]; 
      if($this->form->validate()) { 
       echo 'Form is submited!'; 
      } else 
       echo 'You have incorrectly filled out the form!'; 
      Yii::app()->end(); 
     } 
    } 

    public function run() { 

     $this->render('form',array(
      'model'=>$this->form, 
      'id'=>$this->id, 
      'name'=>$this->name 
     )); 
    } 

} 

這是我的模型的代碼。我已經簡化一切爲了可視性:

class reportBugForm extends CFormModel { 

    public $subject = ''; 
    public $message = ''; 

    public function rules() { 
     return array(
      array('message','required','on'=>'error,question,suggestion'), 
      array('subject','safe','on'=>'question'), 
      array('subject','required','on'=>'error,suggestion') 
     ); 
    } 

    public function attributeLabels() { 
     return array(
      'subject'=>'Subject', 
      'message'=>'Message' 
     ); 
    } 
} 

這裏是我查看文件的代碼:

<div class="modal fade hide" id="<? echo $id; ?>"> 
    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" id="reportBug_iconClose">&times;</button> 
     <h3>Report a bug</h3> 
    </div> 
    <? 
    $form = $this->beginWidget('CActiveForm', array(
     'id'=>$name, 
     'enableAjaxValidation'=>true, 
     'enableClientValidation'=>true, 
     'clientOptions'=>array(
      'validateOnSubmit'=>true, 
      'validateOnChange'=>false, 
      'validateOnType'=>false 
     ), 
     'focus'=>array($model,'subject') 
    )); 
    ?> 
    <div class="modal-body"> 
     <fieldset> 
      <div class="control-group"> 
       <? 
       echo $form->label($model,'subject',array('for'=>'reportBug_subject')); 
       echo $form->textField($model,'subject',array('id'=>'reportBug_subject','placeholder'=>'A short summary of your problem','class'=>'input-xxlarge')); 
       echo $form->error($model,'subject',array('class'=>'help-block error')); 
       ?> 
      </div> 
      <div class="control-group"> 
       <? 
       echo $form->label($model,'message',array('for'=>'reportBug_message')); 
       echo $form->textArea($model,'message',array('id'=>'reportBug_message','class'=>'input-xxlarge','rows'=>5,'placeholder'=>'Please, provide details of found error')); 
       echo $form->error($model,'message',array('class'=>'help-block error')); 
       ?> 
      </div> 
     </fieldset> 
    </div> 
    <div class="modal-footer"> 
     <? 
     echo CHtml::ajaxSubmitButton('Send',null,array(),array('class'=>'btn btn-primary')); 
     echo CHtml::htmlButton('Close',array('id'=>'reportBug_btnClose','class'=>'btn','data-dismiss'=>'modal')); 
     ?> 
    </div> 
    <? $this->endWidget(); ?> 
</div> 

不執行任何客戶端或AJAX驗證!表單由ajax提交,沒有任何客戶端驗證,並返回字符串You have incorrectly filled out the form!。請告訴我它可能是什麼?

P.S.我不知道它可能會有所不同,但是我爲Yii使用了Twitter Bootstrap擴展。

P.S.S.還有一件奇怪的事情。在我的視圖文件中使用CHtml::submitButton時,表單僅由POST提交,當我使用CHtml::ajaxSubmitButton時,它通過Ajax提交併返回上述結果。

+1

你還沒有提到確切的問題。什麼是輸出?你在看什麼/看不見? –

+0

也許你也可以發佈表格本身,所以我們可以仔細看看 – burnedikt

+0

對不起,我遲到的答案!我更新了我的問題以提高可視性。我希望這次我能更好地描述我的問題。請看一下。 – Stas

回答

1

此指導工作,只爲 「安全」 PARAMS:

$this->form->attributes = $_POST[$this->name]; 

您必須添加 「消息」 安全驗證:

array('subject,message','safe','on'=>'question'), 

,或者您必須使用:

$this->form->subject = $_POST[$this->name]['subject']; 
$this->form->message = $_POST[$this->name]['message']; 

查看此文檔: http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules

您的驗證不起作用,因爲「消息」是必需的,但不是「安全的」。

+0

iDexter,你不正確的說,「驗證不起作用,因爲信息是必需的但不安全」。因爲[如果屬性在給定場景中適用的驗證規則中出現時被認爲是安全的](http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-safe-attributes )。 –