在 Yii 中,我怎样才能有一个循环对象数组的表单?

发布于 2024-09-15 00:54:49 字数 1094 浏览 1 评论 0原文

我正在尝试创建一个 Yii ActiveForm 来编辑表格中显示的对象列表中的值。

涉及的类:

class ResultForm extends CFormModel {

    /**
     * @var array[Result]
     */
    public $results; //Filled with an array of Result objects
}

class Result {  
    public $requiredArea;
}

我的观点:

<% $form = $this->beginWidget('CActiveForm'); %>
<table>
   ....
   <% $rowCounter = 0; foreach($resultForm->results as $result): %>
       ...
       <tr>
           ....
           <td>
               <!-- This doesn't work -->
               <% $form->textField($resultForm,
                   "results[$rowCounter]->requiredArea") %>

               <!-- Just displaying the value works -->
               <%= $resultForm->results[$rowCounter]->requiredArea %>
           </td>
           ...
       </tr>
       <% $rowCounter++; endforeach; %>
</table>
<% $this->endWidget(); %>

文本字段被渲染并且 Yii 不会抱怨,但它们不包含正确的值。

有没有办法可以完成这项工作,或者是否有更好的方法来迭代表单中的对象数组?

I'm trying to create a Yii ActiveForm that edits values from a list of objects, presented in a table.

The classes involved:

class ResultForm extends CFormModel {

    /**
     * @var array[Result]
     */
    public $results; //Filled with an array of Result objects
}

class Result {  
    public $requiredArea;
}

My view:

<% $form = $this->beginWidget('CActiveForm'); %>
<table>
   ....
   <% $rowCounter = 0; foreach($resultForm->results as $result): %>
       ...
       <tr>
           ....
           <td>
               <!-- This doesn't work -->
               <% $form->textField($resultForm,
                   "results[$rowCounter]->requiredArea") %>

               <!-- Just displaying the value works -->
               <%= $resultForm->results[$rowCounter]->requiredArea %>
           </td>
           ...
       </tr>
       <% $rowCounter++; endforeach; %>
</table>
<% $this->endWidget(); %>

The text fields are rendered and Yii does not complain, but they do not contain the proper values.

Is there a way I can make this work, or is there a better approach of for iterating through an array of objects in a form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2024-09-22 00:54:49

我认为你想要这个而不是你拥有的:

<% $form->textField($result,"[$rowCounter]requiredArea") %>

你想要做的是传递你正在迭代的模型($result)而不是父/表单模型,并且你想要传递该模型属性的名称作为第二个参数(以及 $i 值/数组索引)而不是实际属性。

查看 Yii 指南中的此页面以获取有关表格输入的更多信息:
http://www.yiiframework.com/doc/guide/form.table

另外,检查 textfield() 的参数应该是什么:
http://www.yiiframework.com/doc/api/CHtml#activeTextField-细节

干杯!

I think you want this instead of what you have:

<% $form->textField($result,"[$rowCounter]requiredArea") %>

What you want to do is pass the model you are iterating over ($result) instead of the parent/form model, and you want to pass in the name of that model's attribute in as the second parameter (along with the $i value/array index) instead of the actual attribute.

Review this page in the Yii guide for more info on tabular input:
http://www.yiiframework.com/doc/guide/form.table

Also, check what the parameters are supposed to be for textfield():
http://www.yiiframework.com/doc/api/CHtml#activeTextField-detail

cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文