Cakephp2 - 从循环创建表单
Cakephp2 - Creating Form from loop
我已经用示例数据翻译了 table:
所以,这个 table 保存着代表自定义翻译文本的记录。
现在我想构建一个表单来编辑一页/表单中的所有这些行。
这是控制代码:
public function translate() {
$this->loadModel('Translate');
$data = $this->Translate->find('all');
$this->set('data', $data);
pr ($this->request->data);
if ($this->request->is('post','put')) {
if ($this->Translate->save($this->request->data)) {
$this->Session->setFlash('Recipe Saved!');
return $this->redirect($this->referer());
}
}
}
查看 - 请注意,我使用循环来创建输入,不确定 cakephp 是否有更好的方法来执行此操作。
<?php echo $this->Form->create('Translate'); ?>
<?php
foreach ($data as $d) {
echo $this->Form->input('text', array('label' => 'Link strony', 'type' => 'text','value'=>$d['Translate']['text']));
echo $this->Form->input('id', array('type' => 'hidden', 'value' => $d['Translate']['id']));
}
?>
<?php echo $this->Form->end(array('class' => 'btn btn-success floatRight', 'label' => 'Zapisz')); ?>
目前,此代码有效,但不如我预期。 $this->request->data
仅显示最后输入,忽略其他输入。在附件中,您可以看到 $this->request->data 的调试。只编辑最后一项。我想要的只是能够编辑选定的输入并保存。感谢您的帮助。
Looks like you're saving multiple rows in a single form. In that case,
you need to change your approach a bit.
- Use proper indices in the Form helper.
- Use saveAll() instead of save() to save multiple data.
对视图文件的更改:
<?php
foreach ($data as $k => $d) {
echo $this->Form->input('Translate.'.$k.'.text', array(
'label' => 'Link strony',
'type' => 'text',
'value' =>$d['Translate']['text']
));
echo $this->Form->input('Translate.'.$k.'.id', array(
'type' => 'hidden',
'value' => $d['Translate']['id']
));
}
?>
然后,在您的控制器中:
if ($this->request->is('post','put')) {
$this->Translate->saveAll($this->request->data['Translate']);
/* Other code */
}
尝试将名称指定为数组 (translate[]) :
echo $this->Form->input('text', array('label' => 'Link strony', 'type' => 'text','value'=>$d['Translate']['text'],'name'=>'translate[]'));
我已经用示例数据翻译了 table:
所以,这个 table 保存着代表自定义翻译文本的记录。
现在我想构建一个表单来编辑一页/表单中的所有这些行。
这是控制代码:
public function translate() {
$this->loadModel('Translate');
$data = $this->Translate->find('all');
$this->set('data', $data);
pr ($this->request->data);
if ($this->request->is('post','put')) {
if ($this->Translate->save($this->request->data)) {
$this->Session->setFlash('Recipe Saved!');
return $this->redirect($this->referer());
}
}
}
查看 - 请注意,我使用循环来创建输入,不确定 cakephp 是否有更好的方法来执行此操作。
<?php echo $this->Form->create('Translate'); ?>
<?php
foreach ($data as $d) {
echo $this->Form->input('text', array('label' => 'Link strony', 'type' => 'text','value'=>$d['Translate']['text']));
echo $this->Form->input('id', array('type' => 'hidden', 'value' => $d['Translate']['id']));
}
?>
<?php echo $this->Form->end(array('class' => 'btn btn-success floatRight', 'label' => 'Zapisz')); ?>
目前,此代码有效,但不如我预期。 $this->request->data
仅显示最后输入,忽略其他输入。在附件中,您可以看到 $this->request->data 的调试。只编辑最后一项。我想要的只是能够编辑选定的输入并保存。感谢您的帮助。
Looks like you're saving multiple rows in a single form. In that case, you need to change your approach a bit.
- Use proper indices in the Form helper.
- Use saveAll() instead of save() to save multiple data.
对视图文件的更改:
<?php
foreach ($data as $k => $d) {
echo $this->Form->input('Translate.'.$k.'.text', array(
'label' => 'Link strony',
'type' => 'text',
'value' =>$d['Translate']['text']
));
echo $this->Form->input('Translate.'.$k.'.id', array(
'type' => 'hidden',
'value' => $d['Translate']['id']
));
}
?>
然后,在您的控制器中:
if ($this->request->is('post','put')) {
$this->Translate->saveAll($this->request->data['Translate']);
/* Other code */
}
尝试将名称指定为数组 (translate[]) :
echo $this->Form->input('text', array('label' => 'Link strony', 'type' => 'text','value'=>$d['Translate']['text'],'name'=>'translate[]'));