Javascript 如何在cakephp中使用jquery/ajax验证?

Javascript 如何在cakephp中使用jquery/ajax验证?,javascript,ajax,validation,cakephp,Javascript,Ajax,Validation,Cakephp,我已经嵌入了jquery,它正在工作..但是这个查询有一个小问题。。。当提交空表单时,它会显示带有字段的错误以及没有错误的表单…我的意思是。。它显示双输入字段。。。这背后的主要问题是什么。。。? 以下是控制器操作: public function add() { if ($this->request->is('post')) { if(!empty($this->data)){ $this->Post

我已经嵌入了jquery,它正在工作..但是这个查询有一个小问题。。。当提交空表单时,它会显示带有字段的错误以及没有错误的表单…我的意思是。。它显示双输入字段。。。这背后的主要问题是什么。。。? 以下是控制器操作:

    public function add() {
        if ($this->request->is('post')) {
            if(!empty($this->data)){
            $this->Post->create();
            if ($this->Post->save($this->data)) {
                if($this->RequestHandler->isAjax()){
                    $this->render('success','ajax');

                }
                else{
                $this->Session->setFlash(__('Your post has been saved.'));
                 $this->redirect(array('action' => 'index'));
                }
            }
            $this->Session->setFlash(__('Unable to add your post.'));
        }
        }
    }
 public function validate_form()
    {
        if($this->RequestHabdler->isAjax())
        {
            $this->data['Post'][$this->params['form']['field']]=$this->params['form']['value'];
            $this->Post->set($this->data);
            if($this->Post->validates()){
                $this->autoRender=false;

            }
            else {

                $error=$this->validateErrors($this->Post);
                $this->set('error',$error[$this->params['form']['field']]);
            }
        }

    }
以下是验证js文件:

$(document).ready(function (){

    $('#title').blur(function(){
        $.post(
        '/cakephp/posts/validate_form',
        {field:$('#title').attr('id'),value:$('#title').val()},
        handleTitleValidation
        );

        function handleTitleValidation(error)
        {
            if(error.length>0){

                if($('#title-notEmpty').length==0){

                    $('#sending').after('<div id="notEmpty" class="error-message">'+error+"</div>");
                }
                            }
                            $('#title-notEmpty').remove();

        }
    });
    $('#body').blur(function(){
        $.post(
        '/cakephp/posts/validate_form',
        {field:$('#body').attr('id'),value:$('#body').val()},
        handleBodyValidation
        );

        function handleBodyValidation(error)
        {
            if(error.length>0){

                if($('#body-notEmpty').length==0){

                    $('#sending').after('<div id="notEmpty" class="error-message">'+error+"</div>");
                }
                            }
                           $('#body-notEmpty').remove();

        }
    });

});
这是add.ctp

<!-- File: /app/View/Posts/add.ctp -->
<?php echo $this->Html->script('jquery',false);?> 
<?php echo $this->Html->script('validation',false);?> 
<div id="success" style="background-color: lightgreen;"></div>
<h1>Add Post</h1>
<?php
echo $this->Form->create('Post');
echo $this->Form->input('title', array('id'=>'title'));
echo $this->Form->input('body', array('rows' => '3','id'=>'body'));
echo $this->Js->submit('Save Post', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeOut'),
'update'=>'#success'
) );
?>
    <div id="sending" style="display:none;">
        <?php echo $this->Html->image('ajax-loader.gif', array('alt' => 'Loading...')); ?>
    </div>
这就是我得到的。。。html源代码:

<form id="PostAddForm" accept-charset="utf-8" method="post" action="/cb/cakephp/posts/add">
<div style="display:none;">
<input type="hidden" value="POST" name="_method">
</div>
<div class="input text required error">
<label for="title">Title</label>
<input id="title" class="form-error" type="text" required="required" value="" maxlength="50" name="data[Post][title]">
<div class="error-message">This field cannot be left blank</div>
</div>
<div class="input textarea required error">
<label for="body">Body</label>
<textarea id="body" class="form-error" required="required" cols="30" rows="3" name="data[Post][body]"></textarea>
<div class="error-message">This field cannot be left blank</div>
</div>
<div class="submit">
<input id="submit-1060465790" type="submit" value="Save Post">
</div>
<div id="sending" style="display:none;">
</form>
</div>
<h1>Add Post</h1>
<form id="PostAddForm" accept-charset="utf-8" method="post" action="/cb/cakephp/posts/add">
<div style="display:none;">
<div class="input text required">
<label for="title">Title</label>
<input id="title" type="text" required="required" maxlength="50" name="data[Post][title]">
</div>
<div class="input textarea required">
<label for="body">Body</label>
<textarea id="body" required="required" cols="30" rows="3" name="data[Post][body]"></textarea>
</div>
<div class="submit">
<input id="submit-1878163930" type="submit" value="Save Post">

.... 我正在学习这个教程。。。。我是不是漏掉了什么???你有打字错误。将RequestHabdler更改为RequestHandler@gerl改变了,但效果。。。同样的结果。。。