0

我有 2 个相关的模型,我想做的是在我的添加视图中我想将 id 字段设置为隐藏字段,以便它不会显示在我的添加视图中,当我将其设置为隐藏时表单不会不提交。有没有办法将 id 值作为隐藏字段传递?

这是我的添加视图

<?php echo $this->Form->create('ItQueryComment'); ?>
<?php echo __('Add It Query Comment'); ?>
<?php
echo $this->Form->hidden('it_query_id');
echo $this->Form->input('comment');
?>

<?php echo $this->Form->end(__('Submit')); ?>

这是 ItQueries 控制器的添加功能

public function add() {
if ($this->request->is('post')) {
$this->ItQuery->create();
if ($this->ItQuery->save($this->request->data)) {
$this->Session->setFlash(__('The it query has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The it query could not be saved. Please, try again.'));
}
}
$itQueryTypes = $this->ItQuery->ItQueryType->find('list');
$this->set(compact('itQueryTypes'));
}

先感谢您

4

1 回答 1

2

您不会将它们添加到视图表单中,然后只是再次将它们传递给控制器​​。

尝试在保存之前将它们添加到数据数组中:

if ($this->request->is('post')) {
    $this->ItQuery->create();
    // add the content before passing it on to the model
    $this->request->data['ItQuery']['it_query_id'] = $id;
    if ($this->ItQuery->save($this->request->data)) {
        ... 
    }
}

表单不必知道它们。没有人可以阅读或篡改它们。

请参阅“默认值 - 隐藏!” 在http://www.dereuromark.de/2010/06/23/working-with-forms/

于 2013-03-07T12:38:26.740 回答