0

在我的控制器中,我有 add($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);  
  }  

}
添加.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)); ?>    

4

2 回答 2

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

基本上将id您需要的参数作为参数传递给表单。这样,当您提交并且验证失败时,蛋糕会重定向到正确的 URL(即使用您需要的 id 作为参数)

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

于 2012-06-01T09:48:06.423 回答
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);  
  }  
于 2012-06-01T08:43:04.287 回答