在我的控制器中,我添加了($ id = null)函数。如果iput add / 43则有效。但是,如果任何验证失败它重新加载没有添加/ 43。 (显示错误消息后)。 怎么办?1.3版 控制器:
function add($id = null) {
if (!empty($this->data)) {
$this->ConsultingDet->create();
if ($this->ConsultingDet->save($this->data)) {
$this->Session->setFlash('The consulting det has been saved', true);
$this->redirect(array('controller' => 'patients', 'action' => 'home'));
} else {
$this->Session->setFlash('The consulting det could not be saved. Please, try again.', true);
}
}
if (is_null($id)) {
$this->set('id', 0);
}
}
add.ctp:
<div class="consultingDets form">
<?php echo $this->Form->create('ConsultingDet');?>
<fieldset>
<div class="sub1">
<?php
echo $this->Form->input('patient_id', array('type' => 'hidden', 'value' => $patient['Patient']['id']));
if ($id>0) //This (id) brings out error on redirection
echo $this->Form->input('id',array('type' => 'hidden', 'value' => $id));
echo $this->Form->input('temperature');
?>
</div>
</fieldset>
<?php echo $this->Form->end(__('Submit', true)); ?>
答案 0 :(得分:1)
<?php echo $this->Form->create(null, array('url' =>
array('controller' => 'consultingdets', 'action' => 'add', $id))); ?>
//<form action="consultingdets/add/43"
基本上将您需要的id
作为参数传递给表单。这种方式,当你提交和验证失败时,蛋糕重定向到正确的URL(即你需要作为参数的id)
您的控制器名称可能有所不同。
答案 1 :(得分:0)
你可以试试这个:
function add($id = null) {
if (!empty($this->data)) {
$this->ConsultingDet->create();
if ($this->ConsultingDet->save($this->data)) {
$this->Session->setFlash('The consulting det has been saved', true);
$this->redirect(array('controller' => 'patients', 'action' => 'home'));
} else {
$this->Session->setFlash('The consulting det could not be saved. Please, try again.', true);
//redirect to referrer page
$this->redirect($this->referer());
}
}
if (is_null($id)) {
$this->set('id', 0);
}