CakePHP 根据相关模型上的现有选择预填充选择框
我试图在编辑记录时使用现有选择预先填充此状态选择框。 在相关模型中。
plan_details/view.ctp
echo $this->Form->input('State',array('empty' => false,'options' => $state));
plan_details_controller视图函数:
$state = $this->PlanDetail->Plan->State->find('list');
$this->set(compact('state', $state));
debug($state);
view.ctp中的数组输出(如预期):
Array
(
[1] => Oregon
[2] => Washington
)
上面的选择框默认为1大批。我需要它默认为已经存在的选定值。
例如,当我添加一条记录和选定的 Washington(2) 时,当查看我的编辑屏幕时,预选的值应该是 Washington,值为 2。
我被卡住了,并为此苦恼了一段时间。知道我做错了什么吗?
I am trying to pre-populate this State select box with the existing selection when editing records.
in a related model.
plan_details/view.ctp
echo $this->Form->input('State',array('empty' => false,'options' => $state));
plan_details_controller view function:
$state = $this->PlanDetail->Plan->State->find('list');
$this->set(compact('state', $state));
debug($state);
Array output in view.ctp (as expected):
Array
(
[1] => Oregon
[2] => Washington
)
My select box above defaults to 1 in the array. I need it to default to the already existing selected value.
For example, when I added a record and a selected Washington(2), then when viewing my edit screen, the pre-selected value should be Washington with value 2.
I'm stuck and cracking at this for a while. Any idea what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在编辑页面,则需要设置 $this->data 以自动填充它。在您的编辑操作中,
这应该在记录中读取,然后(注意复数)
在您的表单中,它应该读取
我发现您的方法有点令人困惑,但上面的方法应该有效。此表单用于编辑计划、计划详细信息或两者
If you are editing a page you need to set $this->data to auto populate it. Within your edit action
This should read in the record then ( note the plural )
In your form it should read
I find your approach a bit confusing but the above should work. Is this form for editing a Plan, PlanDetail or both