【问题标题】:cakePHP - Associated Model validationcakePHP - 关联模型验证
【发布时间】:2014-03-13 20:58:39
【问题描述】:

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

这是验证方法

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)

【问题讨论】:

  • @MattDiamant 感谢您的编辑。但是你能帮我解决这个问题吗?非常感谢

标签: php jquery ajax validation cakephp


【解决方案1】:

哦,天哪,过去 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'];'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多