2012-06-01 53 views
0

在我的控制器中,我添加了($ id = null)函数。如果iput添加/ 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

因为你会渲染视图添加?如果验证是错误的,您可以重定向到引用者。 – Bob

+0

也许是因为你没有设置id(顺便说一句,它非常罕见,试图添加一个没有被验证过的对象) – Yises

回答

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); 
    } 
+0

在这种情况下,FLash消息即将到来,但验证消息没有出现。 – alexkd

+0

对不起,重定向不是要走的路。 :) – Bob

1
<?php echo $this->Form->create(null, array('url' => 
      array('controller' => 'consultingdets', 'action' => 'add', $id))); ?> 
//<form action="consultingdets/add/43" 

基本上通过id你需要作为参数的形式。通过这种方式,当您提交并验证失败时,Cake重定向到正确的URL(即使用您需要的id作为参数)

您的控制器名称可能有所不同。

+0

仍然错误即将到来。表单行为=“consultingdets/add/43”表单在查看源页面时(在使用您的答案之前)已经显示。 – alexkd

+0

如果($ id> 0){} -in add.ctp-在这里显示'undefined variable' – alexkd

+0

@alexkd这将解决您的问题,重定向时会出现什么问题(验证失败后)。 – Bob