我試圖在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。任何幫助將不勝感激。
也可以'pr($ this-> request);'或'pr($ this);' – Bob 2012-04-20 10:37:06
//不驗證邏輯'$ this-> set('error',$ error [$ this-> request ['data '] ['field']] [0]);' – Bob 2012-04-20 17:55:52
我將'表單'更改爲'數據',並且它工作正常:D感謝堆。 – 2012-04-22 21:39:53