我正在尝试使用在 CakePHP 2.1 中为联系表单工作的 Ajax 和 jQuery 验证错误。在名称字段模糊时,调用 js 函数:
$(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 还很陌生。任何帮助将不胜感激。