2012-08-06 37 views
1

模型

class CompanyCategory extends AppModel 
{ 
    public $name = "CompanyCategory"; 

    public $hasMany = array(
     "Company" 
    ); 
} 

控制器

public function admin_edit($id = null){ 

      //debug($this->request); 
      //exit(0); 
      if($id == null){ 
       $this->Session->setFlash("ID categorie eronat!", "flash/simpla_error"); 
       $this->redirect("index"); 
      } 
      if($this->request->is('post')){ 
       if($this->CompanyCategory->save($this->request->data)){ 
        $this->Session->setFlash("Categoria a fost salvata!", "flash/simpla_success"); 
       } 
       else{ 
        $this->Session->setFlash("Categoria NU a fost salvata!", "flash/simpla_error"); 
       } 
      } 
      else{ 
       $this->Session->setFlash("READ!", "flash/simpla_error"); 
       $this->request->data = $this->CompanyCategory->read(null, $id); 
      } 
     } 

的VIEW

<div class="content-box"> 
    <div class="content-box-header"> 
     <h3>Editeaza categorie firme</h3> 
    </div> 
    <div class="content-box-content"> 
     <?php 
      echo $this->Form->create("CompanyCategory", array(
       'inputDefaults' => array(
        'error' => array(
         'attributes' => array(
          'wrap' => 'span', 
          'class' => 'input-notification error png_bg' 
         )     
        ) 
       ) 
      )); 
     ?> 

     <?=$this->Form->input('id', array('type' => 'hidden'))?> 

     <?=$this->Form->input('title', array('class' => "text-input small-input", 'label' => 'Denumire'))?> 

     <?=$this->Form->submit('Salveaza', array('class' => "button"))?> 
    </div> 
</div> 

我的問題當提交表單時,控制器返回false for request-> is('false');

如果我明確地在視圖中爲create方法中的表單助手設置了類型'post',它按預期工作。

這是有點令人沮喪,而表單方法已經發布沒有設置它。

我做錯了什麼?

+0

嘗試request'使用'print_r'顯示'$這個 - >的價值。我有一個類似的問題,並由於一些奇怪的原因使用的提交方法是PUT而不是POST(你的看法似乎很好,但)。 – pollirrata 2012-08-06 16:54:54

回答

1

使用該控制器

public function admin_edit($id = null) { 
     $this->layout = 'admin_layout'; 
     $this->CompanyCategory->id = $id; 
     if (!$this->CompanyCategory->exists()) { 
      throw new NotFoundException(__('Invalid CompanyCategory model')); 
     } 
     if ($this->request->is('post') || $this->request->is('put')) { 
      if ($this->CompanyCategory->save($this->request->data)) { 
       $this->Session->setFlash(__('The CompanyCategory model has been saved')); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The CompanyCategory model could not be saved. Please, try again.')); 
      } 
     } else { 
      $this->request->data = $this->CompanyCategory->read(null, $id); 
     } 

    } 
+0

是的,阿比德,我剛剛看到Cake使用由表單助手生成的隱藏字段來設置/識別方法的事實。在添加表單中,其中request-> data爲null的情況下,名稱爲「_method」值的隱藏字段的值爲「POST」,在編輯表單中,其中request-> data是由read()方法創建的數組模型,值是「PUT」。感謝你的回答! – Michael 2012-08-06 17:05:18

+0

我認爲它也可以在條件中使用以下內容:(!empty($ this-> request-> data))。它更短。 – Michael 2012-08-06 17:08:31

-1

我不知道你使用的是什麼框架,這一點,但我的猜測是,在視圖中的 $這個 - >形式 - >創建()

應該有一個選項,您可以設置要發佈的表單操作類型。

如果你查看那裏的PHP代碼生成的HTML表單是它創建的動作屬性?我的猜測是它可能在默認情況下做GET。

相關問題