在 Yii 中,我怎样才能有一个循环对象数组的表单?
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你想要这个而不是你拥有的:
你想要做的是传递你正在迭代的模型($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:
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!