2012-12-31 158 views
1

我目前正在使用CakePHP,並且遇到未將數據提交到數據庫的表單的問題。沒有錯誤消息,當我調試時,表單數據似乎被添加到數組中(請參閱附加圖像)。表單不提交數據

這是我的模型:

類拆分擴展AppModel {

public $validate = array(
    'title' => array('rule' => 'notEmpty'), 
    'url' => array('rule' => 'notEmpty'), 
    'descr' => array('rule' => 'notEmpty') 
); 

這是我的看法:

<h1>Add New Case</h1> 

<?php 
pr($this->request->data); 
echo $this->Form->create('Split'); 
echo $this->Form->input('title'); 
echo $this->Form->input('url'); 
echo $this->Form->input('descr', array('rows' => '2')); 
echo $this->Form->end('Add New Case'); 
?> 

這是在控制器中的附加功能:

public function add() { 

    if ($this->request->is('split')) { 

     $this->Split->create(); 
     if ($this->Split->save($this->request->data)) { 
      $this->Session->setFlash('Your case has been saved.'); 
      $this->redirect(array('action' => 'index')); 

     } else { 
      $this->Session->setFlash('Unable to add case.'); 
     } 
    } 

} 

Afte [R按提交的數據插入到拆分陣列,但形式不提交:

After pressing submit

這是呈現出來的HTML:

<form action="/uilab/splits/add" id="SplitAddForm" method="post" accept-charset="utf-8"> 

<div style="display:none;"><input type="hidden" name="_method" value="POST"></div> 

<div class="input text required"> 

    <label for="SplitTitle">Title</label> 

    <input name="data[Split][title]" maxlength="255" type="text" value="Some title" id="SplitTitle"></div> 

    <div class="input text required"> 
     <label for="SplitUrl">Url</label> 
     <input name="data[Split][url]" maxlength="255" type="text" value="http://www.someurl.com" id="SplitUrl"> 
    </div> 

    <div class="input textarea required"> 
     <label for="SplitDescr">Descr</label> 
      <textarea name="data[Split][descr]" rows="2" cols="30" id="SplitDescr">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</textarea> 
     </div> 

    <div class="submit"><input type="submit" value="Add New Case"></div> 

</form> 

我是很新,CakePHP的所以任何關於如何解決這個問題的想法都非常感謝。

哦,新年快樂btw! :)

回答

2
if ($this->request->is('split')) { 

應該

if ($this->request->is('post')) { 

閱讀request handling

該請求永遠不會是split,所以代碼永遠不會被執行。有效值爲post, get, put ...

+0

這樣做!非常感謝! :) – timkl