将值从Yii CController类传递到CForm(表单生成器)配置数组

将值从Yii CController类传递到CForm(表单生成器)配置数组,yii,yii-cformmodel,Yii,Yii Cformmodel,我是Yii的新手,我正在尝试以“正确”的方式完成我的初始项目。我创建了一个CFormModel类,它需要三个字段来查询某些数据,一个CForm config来构造表单,一个cControl来将表单绑定在一起(所有内容如下) 数据请求需要一个帐户,这个帐户可以来自几个不同的地方。我认为检索它应该在控制器中。但是,我不知道如何从控制器将其放入表单的隐藏“account”字段,以便在提交后将其放入分配给CFormModel的参数中。更一般地说,我知道如何从CController传递到view脚本,而不

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

数据请求需要一个帐户,这个帐户可以来自几个不同的地方。我认为检索它应该在控制器中。但是,我不知道如何从控制器将其放入表单的隐藏“account”字段,以便在提交后将其放入分配给CFormModel的参数中。更一般地说,我知道如何从CController传递到view脚本,而不是CForm。注册表(
Yii::app()->params[]
)是我的最佳选择吗

我想我可以将它从表单(和必填字段)中删除,然后等待在提交操作(
actionSummaries
)中填充它。这是否违背了CForm的意图?是否有最佳实践?即使采用这种解决方案,是否有人能解决第一个问题,以防它再次出现

欢迎任何其他温和的批评

models/SummariesForm.php

views/account/select.php

views/account/selectForm.php


答案是:不,按你的要求去做。您可以看到
$form
变量,当它从控制器传递到视图时,它的行为几乎像数组。解决方案是将更多属性
$account
添加到
selectformmodel
中,并将其与其他元素一样对待。我认为,如果您还想提交新字段的值,那么将其保留在表单之外并不是一种合适的方式

编辑:

我不明白你在说什么。selectForm.php中的
元素
下已经有一个
帐户
条目。在设置表单以提交到
actionSelect
的操作中,我稍微更改了代码以实例化SummariesForm模型并在那里分配帐户,但我仍然不知道如何在selectForm.php中访问它<找不到code>$account
,selectForm中的
$this
var\u dump
显示
$account
属性隐藏在私有
\u模型
属性后面。我添加了更多的屏幕截图,以解释它在我的思想中的工作方式。您可以使用var_转储$this并查看$account,因为您已经从控制器传递了它,但它从来没有随$form一起出现。因此,我建议您在表单中添加更多属性。
class SummariesForm extends CFormModel
{
    public $account;
    public $userToken;
    public $year;

    public function rules () {...}

    public function fetchSummary () {...}

    static public function getYearOptions () {...}
}
<?php
$this->pageTitle=Yii::app()->name;
?>

<div class="form">
    <?php echo $form->render(); ?>
</div>
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.');
            }
        }
    }

}
<?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',
        ),
    ),
);