如何隐藏id输入并在cakephp中给它一个值

时间:2013-03-07 11:18:27

标签: cakephp

我有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'));
}

提前谢谢

1 个答案:

答案 0 :(得分: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/