2012-04-19 72 views
0

我試圖在CakePHP 2.1中爲聯繫表單工作時獲取Ajax和jQuery的驗證錯誤。在名稱字段的模糊的JS調用函數:CakePHP 2.1 Ajax驗證錯誤

$(document).ready(function(){ 

$('#name').blur(function(){ 
    $.post(
     '/Cake_ajax/Contacts/validate_form', 
     { field: $(this).attr('id'), value: $(this).val() }, 
     handleNameValidation 
    ); 
}); 

function handleNameValidation(error){ 
    if(error.length > 0){ 
     if($('#name-notEmpty').length == 0){ 
      $('#name').after($('<div id="name-notEmpty" class="error-message">' + error + '</div>')); 
     } 
    }else{ 
     $('#name-notEmpty').remove(); 
    } 
} 
}); 

的JavaScript調用validate_form功能在我的控制器:

public function validate_form(){ 
     if($this->RequestHandler->isAjax()){ 
      $this->request->data['Contact'][$this->request->params['form']['field']] = $this->request->params['form']['value']; 
      $this->Contact->set($this->request->data); 
      if($this->Contact->validates()){ 
       $this->autorender = FALSE; // don't render a view 
      }else{ 
       $error = $this->validateErrors($this->Contact); 
       $this->set('error', $error[$this->request->params['form']['field']]); 
      } 
     } 
    } 

在我看來,我得到了幾個錯誤,在錯誤被稱爲:

Undefined index: form [APP\Controller\ContactsController.php 
Undefined index: form [APP\Controller\ContactsController.php 

我在我的智慧結束,我是相當新的CakePHP。任何幫助將不勝感激。

回答

2

在你的控制器,你應該有類似下面。 Cake 2.0替換了RequestHandlerComponent和Controller中的很多功能。它還將所有地方的$ this-> params數組和舊的$ this->數據替換爲$ this-> request-> data,就像那樣。您可以訪問migration guide

public function validate_form(){ 
     if($this->RequestHandler->isAjax()){ 
      $this->request->data['Contact'][$this->request['form']['field']] = $this->request['form']['value']; 
      $this->Contact->set($this->request->data); 
      if($this->Contact->validates()){ 
       $this->autorender = FALSE; // don't render a view 
      }else{ 
      $error = $this->validateErrors($this->Contact); 
      // didn't validate logic 
      $this->set('error',$this->Contact->validationErrors[$this->request['data']['field']][0]); 
      } 
     } 
    } 
+1

也可以'pr($ this-> request);'或'pr($ this);' – Bob 2012-04-20 10:37:06

+1

//不驗證邏輯'$ this-> set('error',$ error [$ this-> request ['data '] ['field']] [0]);' – Bob 2012-04-20 17:55:52

+2

我將'表單'更改爲'數據',並且它工作正常:D感謝堆。 – 2012-04-22 21:39:53

2

嘗試$this->request->params['field'],而不是$this->request->params['form']['field']

或者你檢查isAjax()之後,嘗試$this->request->params做的var_dump。從您收到的錯誤中,表格索引不存在於$this->request->params中。

+0

一點點沒有幫助我害怕。我做的 $這 - >請求 - >數據 一個的var_dump並且它返回: 陣列(2){[ 「字段」] =>串(4) 「名字」[ 「值」] =>串(0)「」} – 2012-04-19 23:56:03

+0

是的 - 看看文檔:http://book.cakephp.org/2.0/en/controllers/request-response.html - 你從ajax文章發出請求,而不是表單提交,所以這些字段不會在表單數組內。希望幫助 – kmfk 2012-04-20 00:01:52

+0

是的,只是刪除'['form']',這應該可以解決你的問題 – Ascherer 2012-04-20 00:04:07

0

如果你想使用標準的形式和蛋糕表單助手時模擬像做了POST,你也可以簡單地命名發佈參數相同的方式,表單助手名稱輸入字段。

$j.post(
    '/Cake_ajax/Contacts/validate_form', 
    { "data[Contact][" + $(this).attr('id') + "]": $(this).val() }, 
    handleNameValidation 
); 

這會自動填充$this->request->data['Contact']['name'],你可能只是註釋此行:

//$this->request->data['Contact'][$this->request->params['form']['field']] = $this->request->params['form']['value'];