cakePHP - 相关模型验证

时间:2014-03-13 20:58:39

标签: php jquery ajax validation cakephp

我有一个validate_form()函数,它通过ajax调用,它应该从属于主模型的动作验证相关模型的输入字段。

这是验证方法

public function validate_form(){
        if($this->RequestHandler->isAjax()){
            $this->request->data['Schedule'][$this->request->data['field']] = $this->request->data['value'];
            $this->Schedule->ScheduleContact->set($this->request->data);
            if($this->Schedule->ScheduleContact->validates()){
                $this->autoRender = false;
            }else{
                $error = $this->validateErrors($this->Schedule->ScheduleContact); //this pulls off all validation 
                //$this->set('debug_param', $error);
                $this->layout= 'ajax';
                echo $error[$this->request->data['field']][0]; //for the ScheduleContact Model, and puts it in an array
                $this->autoRender = false;
            }
        }
    } 

每当触发模糊事件时都会发出ajax请求:

$(document).ready(function(){
    $('#name, #surname, #email, #tel_no, #address, #city').blur(function(e){
        var self = e.target.id;
        $.post(
            '/name_project/schedules/validate_form', // it will submit to the validate_form action
            {field: $(this).attr('id'), value: $(this).val()},function(data){ 
            handleValidation(data,self) // data is the error.
            }
        );

    });

});

请求已经发出并且没有错误。但是验证没有发生,并且validates()条件每次都返回true ..任何想法为什么?感谢

[编辑]

这是请求数据的var_dump

array (size=3)
  'field' => string 'name' (length=4)
  'value' => string '' (length=0)
  'Schedule' => 
    array (size=1)
      'name' => string '' (length=0)

1 个答案:

答案 0 :(得分:0)

哦,我的,过去20分钟,这是多么的白痴。答案在于怀疑的请求数据。

更改了此行

$this->request->data['Schedule'][$this->request->data['field']] = $this->request->data['value'];

到这个

'$this->request->data['ScheduleContact'][$this->request->data['field']] = $this->request->data['value'];'