2011-04-25 33 views
4

我想補充的形式在產品頁面的附加信息請求。這是簡單的形式與名稱,國家,電子郵件和問題領域。簡單的形式,而不模型CakePHP中

我創辦這個tutorial,但我不喜歡的想法,只是針對小型化,我需要作出新的模型和控制器。

所以我products_controller.php

function sendemail(){   
    if(!empty($this->data)){   
    if($this->data['Product']['name'] && $this->data['Product']['country'] && $this->data['Product']['email'] && $this->data['Product']['question']){ 
     // sending email 
     $this->Session->setFlash(__('Thanks, your qustion was send.', true), 'default', array('class' => 'message status')); 
     $this->redirect($this->referer());   
    } else { 
     // validation 
     $this->Session->setFlash(__('Fill all fiels', true), 'default', array('class' => 'message error')); 
     $this->redirect($this->referer());   
    } 
    } else {  
    $this->Session->setFlash(__('Error occurred', true), 'default', array('class' => 'message error')); 
    $this->redirect($this->referer()); 
    }  
} 

在view.ctp

print $this->Form->create('', array('action' => 'sendemail')); 
print $this->Form->input('name',array('label' => __('Name',true).':')); 
print $this->Form->input('country',array('label' => __('Country',true).':')); 
print $this->Form->input('email',array('label' => __('E-mail',true).':')); 
print $this->Form->input('question',array('type' => 'textarea', 'label' => __('Your question',true).':')); 
print $this->Form->end(__('Send', true)); 

,並添加功能sendemail添加表單那麼,有沒有辦法如何正確地驗證表單如果某些字段不是重定向後填充此字段將被設置爲錯誤表單輸入?另外我怎樣才能將所有已填充的字段重新設置爲表單?

感謝您的幫助!

回答

7

你並不需要一個數據庫表,但CakePHPs驗證支持本質上是依賴於模型。您的代碼將通過創建一個虛擬模型,並存儲它的驗證規則比任何其他清潔了許多。

爲了解決使用表,添加var $useTable = false;到你的模型定義。

要驗證而不保存,請致電$this->MyModel->set($this->data);,然後致電$this->MyModel->validates()將執行檢查併爲表單中的自動填充人口填充$this->MyModel->validationErrors