0

我是 Yii 的新手,我正在尝试以“正确”的方式完成我的初始项目。我创建了一个 CFormModel 类,它需要三个字段来查询一些数据,一个 CForm 配置来构造表单,以及一个 CController 来将它绑定在一起(所有内容都在下面给出)。

数据请求需要一个帐户,这可能来自几个不同的地方。我认为检索它应该在控制器中。但是,我不知道如何从控制器将其放入表单的隐藏“帐户”字段中,以便在提交后将其分配给 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',
        ),
    ),
);
4

1 回答 1

1

答案是否定的去做你所要求的。$form当变量从控制器传递到视图时,您可以看到其行为几乎类似于数组。解决方案是您添加更多属性$account并将selectForm model其视为其他元素。如果您还想提交它的值,我认为将新字段留在表单之外不是正确的方法。

编辑: 在此处输入图像描述

于 2013-08-05T02:28:38.213 回答