2014-03-13 114 views
0

我有一個validate_form()函數其通過AJAX調用,這應該驗證從屬於主模型的動作相關聯的模型的輸入字段..CakePHP的 - 關聯的模型驗證

這是驗證方法

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。任何想法爲什麼?由於

[編輯]

這是請求數據

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

@MattDiamant感謝編輯。但是,你能幫我解決這個問題嗎?謝謝一堆 – LogixMaster

回答

0

噢,我的,什麼白癡我一直這些過去的20分鐘一個的var_dump。答案在於懷疑請求數據。

改變了這一行

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

這一個

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