簡體   English   中英

cakePHP-關聯模型驗證

[英]cakePHP - Associated Model validation

我有一個通過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)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM