2013-08-04 10 views
0

我是Yii的新手,我試圖以「正確」的方式完成我的初始項目。我創建了一個CFormModel類,它需要三個字段來查詢一些數據,一個CForm配置來構造表單,還有一個CController來將它們連接在一起(所有這些都在下面給出)。從Yii CController類傳遞值到CForm(Form Builder)配置數組

數據請求需要一個帳戶,這可能來自幾個不同的地方。我認爲檢索它應該在控制器中。但是,我不知道如何從控制器中將它隱藏到窗體的隱藏「帳戶」字段中,以便它在提交後將其分配給CFormModel的參數。更一般地說,我知道如何從CController傳遞查看腳本,但不知道CForm。註冊表(Yii::app()->params[])是我最好的選擇嗎?

我想我可以將它從表單(和必填字段)中刪除,然後等待在提交操作中填充它(actionSummaries)。這是否打破了CForm的意圖?有最佳做法嗎?即使採取這種解決方案,有人可以解決第一個問題,以防萬一它再次出現?

任何其他,溫和的批評是值得歡迎的。

模型/ SummariesForm.php

class SummariesForm extends CFormModel 
{ 
    public $account; 
    public $userToken; 
    public $year; 

    public function rules() {...} 

    public function fetchSummary() {...} 

    static public function getYearOptions() {...} 
} 

視圖/帳戶/ select.php

<?php 
$this->pageTitle=Yii::app()->name; 
?> 

<div class="form"> 
    <?php echo $form->render(); ?> 
</div> 

控制器/ AccountController.php

class AccountController extends CController 
{ 
    public $layout = 'extranet'; 

    public function actionSelect() 
    { 
     $model = new SummariesForm(); 

     // retrieve account 
     require_once 'AccountCookie.php'; 

     /* 
     * 
     * Here, I insert the account directly into the 
     * model used to build the form, but $model isn't 
     * available to selectForm.php. So, it doesn't 
     * become part of the form, and this $model doesn't 
     * persist to actionSummaries(). 
     * 
     */ 
     $model->account = AccountCookie::decrypt(); 
     if ($model->account === false) { 
      throw new Exception('Unable to retrieve account.'); 
     } 

     $form = new CForm('application.views.account.selectForm', $model); 
     $this->render('select', array(
      'form' => $form, 
      'account' => $model->account, 
     )); 
    } 

    public function actionSummaries() 
    { 
     $model = new SummariesForm(); 
     if (isset($_POST['SummariesForm'])) { 
      $model->attributes = $_POST['SummariesForm']; 
      /* 
      * 
      * Should I just omit "account" from the form altogether 
      * and fetch it here? Does that break the "model"? 
      * 
      */ 
       if ($model->validate() === true) { 
       try { 
        $summaries = $model->fetchSummary(); 
       } catch (Exception $e) { 
        ... 
        CApplication::end(); 
       } 

       if (count($summaries) === 0) { 
        $this->render('nodata'); 
        CApplication::end(); 
       } 

       $this->render('summaries', array('model' => $model, 'summaries' => $summaries)); 
      } else { 
       throw new Exception('Invalid year.'); 
      } 
     } 
    } 

} 

的意見/帳號/ selectForm.php

<?php 
return array(
    'title' => 'Select year', 
    'action' => Yii::app()->createUrl('Account/Summaries'), 
    'method' => 'post', 

    'elements' => array(
     'account' => array(
      'type' => 'hidden', 
      'value' => $account, 
     ), 
     'userToken' => array(
      'type' => 'hidden', 
      'value' => /* get token */, 
     ), 
     'year' => array(
      'type' => 'dropdownlist', 
      'items' => SummariesForm::getYearOptions(), 
     ), 
    ), 

    'buttons' => array(
     'view' => array(
      'type' => 'submit', 
      'label' => 'View summaries', 
     ), 
    ), 
); 

回答

1

的答案是否定的,做你問什麼。你可以看到$form變量,當它從控制器傳遞到視圖時,它幾乎和數組類似。解決方法是將更多屬性$account添加到selectForm model,並像其他元素一樣對待它。如果你想提交它的價值,我認爲不把這個新領域放在表格之外也是正確的。

編輯: enter image description here

+0

我聽不懂你在說什麼。 selectForm.php的'elements'下已經有一個'account'條目。在設置提交給actionSelect的表單的操作中,我改變了一些代碼來實例化SummariesForm模型並在那裏分配帳戶,但我仍然不知道如何在selectForm.php中訪問它。沒有找到'$ account',並且selectForm中'$ this'的'var_dump'顯示'$ account'屬性隱藏在私有'_model'屬性後面。 – N13

+0

我已經添加了更多的截圖,以更多地解釋它在我的思想中的工作方式。你var_dump $ this並且看到$ account,因爲你已經從控制器傳遞了它,但它永遠不會與$ form一起出現。所以我建議你在表單中添加更多的屬性。 –