Cakephp 添加函数时出错
在我的控制器中,我有add($id=null)函数。如果输入add/43,则它可以工作。但如果任何验证失败,它将重新加载而不添加/43。(显示错误消息后)。 怎么办?。。版本1.3 控制器:Cakephp 添加函数时出错,cakephp,Cakephp,在我的控制器中,我有add($id=null)函数。如果输入add/43,则它可以工作。但如果任何验证失败,它将重新加载而不添加/43。(显示错误消息后)。 怎么办?。。版本1.3 控制器: function add($id = null) { if (!empty($this->data)) { $this->ConsultingDet->create(); if ($this->Consultin
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);
}
<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)); ?>
}add.ctp:
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);
}
<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)); ?>
您可以尝试以下方法:
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);
}
//因为您将渲染视图添加?如果验证为false,您可以重定向到推荐人。可能是因为您没有设置id(顺便说一句,尝试添加由于验证而未创建的对象是非常罕见的…)在这种情况下,FLash消息将出现,但验证消息未出现。抱歉,重定向不是解决方法。:)错误仍然在发生。form action=“consultingdets/add/43”在查看源页面时(在使用答案之前),表单已经显示为这样。如果($id>0){}-in add.ctp-此处显示“undefined variable”@alexkd,则重定向(验证失败后)时会出现什么问题。这将解决您的问题。$this->set('id',$id)
在您的add函数中。当我添加set('id',$id)时,当id不为空时,您忘了将id传递给视图。事情变得成功了,谢谢…但是为什么在setFlash上,在保存失败后id变为空?。