2013-03-19 69 views
0

不知道我在做什麼錯,重新閱讀文檔多次。這是我試圖從外部控制器驗證模型的唯一時間。它處理就好像驗證過程一樣正常,即使我使用了無效的電子郵件地址。只有recaptcha工作;任何幫助將不勝感激:cakephp沒有按預期驗證模型

if(!$this->request->is('get')) 
{ 
    App::import('Vendor','recaptchalib'); 
    $this->set('captchaContent' , recaptcha_get_html($this->publicKey)); 
    $resp = recaptcha_check_answer($this->privateKey, 
     $_SERVER['REMOTE_ADDR'], 
     $this->request->data['recaptcha_challenge_field'], 
     $this->request->data['recaptcha_response_field']);   
    if (!$resp->is_valid) { 
     $this->set('recaptcha_error','You did not enter the words correctly. Please try again.'); 
    } elseif($this->Support->validates($this->request->data)) { 
     // send the message 
    } 
} 
模型

class Support extends AppModel 
{ 
    public $name = 'Support'; 
    public $useTable = false; 
    public $validate = array(
     'email' => array('rule'=>'email','message'=>'You must enter a valid email') 
     ); 
} 
視圖

echo $this->Form->create('Support'); 
echo $this->Form->input('name'); 
echo $this->Form->input('email'); 
echo $this->Form->input('message',array('type'=>'textarea')); 
echo '<div style="margin-left: 150px; margin-bottom: 10px;">'.$captchaContent.'</div>'; 
if($recaptcha_error) echo '<p style="color:red; margin-left: 150px;">'.$recaptcha_error.'</p>'; 
echo $this->Form->end('Send Message'); 

回答

2

發現問題,把

$this->Support->set($this->request->data); 

驗證調用之前需要,顯然是不控制訪問自己的模式需要有此明確設置。無論如何,謝謝:)

+1

接受你的答案請;)顯然,一個控制器不能訪問自己的模型需要明確設置這一點 - 蛋糕永遠不會自動設置模型數據,你總是要自己做。 – AD7six 2013-03-20 01:02:15