我在我的网站上有一个使用 Js 助手的基本 ajax 表单,它工作正常,但是当出现验证错误时,更新回调会在我的成功 div 中复制整个页面。
成功分区:
<div id="success"></div>
提交按钮:
echo $this->Js->submit('Send', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeOut'),
'update'=>'#success',
'class'=>'btn btn-primary'
));
Javascript:
$(document).ready(function(){
$('#postkey, #gender, #hair, #location, #message').blur(function(){
$.post(
'/Cake_ajax/Posts/validate_form',
{ field: $(this).attr('id'), value: $(this).val() }
//handleNameValidation
);
});
});
在我的控制器中验证表单功能:
public function validate_form(){
if($this->RequestHandler->isAjax()){
$this->request->data['Post'][$this->request['data']['field']] = $this->request['data']['value'];
$this->Post->set($this->request->data);
if($this->Post->validates()){
$this->autorender = FALSE; // don't render a view
$this->set('error','');
}else{
$this->layout ="ajax";
$this->autoRender = FALSE;
$error = $this->validateErrors($this->Post);
$this->set('error',$this->Post->validationErrors[$this->request['data']['field']][0]);
}
}
}
我不知道这些信息是否足够继续,如果我需要发布更多代码,请告诉我。
谢谢。