CakePHP 2.1 Ajax验证错误 [英] CakePHP 2.1 Ajax validation errors

查看:114
本文介绍了CakePHP 2.1 Ajax验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得验证错误与Ajax和jQuery工作在CakePHP 2.1中的联系人窗体。在名称字段模糊时调用js函数:

I'm trying to get validation errors with Ajax and jQuery working in CakePHP 2.1 for a contact form. On blur of the name field a js function is called:

$(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函数:

The javascript calls the validate_form function in my controller:

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']]);
            }
        }
    }

调用错误时会出现一些错误:

In my view I'm getting a couple of errors when the error is called:

Undefined index: form [APP\Controller\ContactsController.php
Undefined index: form [APP\Controller\ContactsController.php

我的智慧结束了,我是CakePHP的新手。任何帮助将非常感谢。

I'm at my wits end, and I'm fairly new to CakePHP. Any help would be greatly appreciated.

推荐答案

在您的控制器中,应该有如下所示。 Cake 2.0替换了RequestHandlerComponent和Controller中的许多功能。它也将所有地方的$ this-> params数组和旧的$ this-> data替换为$ this-> request-> data,就像这样。您可以访问迁移指南

In your controller you should have something like below. Cake 2.0 replaces many features in RequestHandlerComponent and Controller. It also replaces $this->params array in all places and the old $this->data to $this->request->data, something like that. You can visit the migration guide.

    public function validate_form(){
        if($this->RequestHandler->isAjax()){
           $this->request->data['Contact'][$this->request['form']['field']] = $this->request['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);
             // didn't validate logic
             $this->set('error',$this->Contact->validationErrors[$this->request['data']['field']][0]);
          }
       }
    }

这篇关于CakePHP 2.1 Ajax验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆