【发布时间】:2012-05-07 18:04:57
【问题描述】:
我的网站上有一个基本的 ajax 表单,它使用 Js 助手,它工作正常,但是当出现验证错误时,更新回调会在我的成功 div 中复制整个页面。
成功 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]);
}
}
}
我不知道这些信息是否足够继续,如果我需要发布更多代码,请告诉我。
谢谢。
【问题讨论】:
-
如果这是直接复制粘贴,您是否尝试将行更改为
$this->autoRender = false(带有大写字母R)?另外,默认使用哪种布局? -
我认为[CakePHP Form Validation with Ajax Using jQuery][1] 将有助于在 CakePhp [1] 中设置 ajax 验证:jamnite.blogspot.com/2009/05/…
-
干杯!!我正在寻找相同问题的解决方案,而您接受的答案解决了我的问题。
-
当您在谷歌上搜索您遇到的问题并且有人遇到相同的问题并且已经解决时,这种感觉真是太棒了:) 祝您的项目好运。
标签: php ajax cakephp cakephp-2.1