2013-02-09 56 views
-1

我在寫一個聯繫表單並想添加一些簡單的驗證例程。該頁面的操作是這樣的:CakePHP沒有檢查表單驗證

public function contact() { 
    $this->loadModel('Contact'); 

    $this->set('pageTitle', 'Contact me'); 
} 

Contact模式是這樣的:

<?php 

class Contact extends AppModel { 
public $useTable = false; 

public $validate = array(
    'name' => array(
     'between' => array(
      'rule' => array('between', 1, 60), 
      'message' => 'Between 1 and 60 characters in length' 
     ) 
    ), 
    'email' => array(
     'kosher' => array(
      'rule' => 'email', 
      'message' => 'Please make sure your email is entered correctly' 
     ), 
    ), 
    'message' => array(
     'between' => array(
      'rule' => array('between', 1, 65000), 
      'message' => 'Between 1 and 65000 characters in length' 
     ) 
    ) 
); 

}

,最後我的看法頁:

<?php echo $this->Form->create('Contact'); ?> 
<?php echo $this->Form->input('name'); ?> 
<?php echo $this->Form->input('email'); ?> 
<?php echo $this->Form->input('message', array('type' => 'textarea')); ?> 
<?php echo $this->Form->end(array('label' => 'Send', 'class' => 'btn btn-primary')); ?> 

然而,當我用不正確的值提交表單時,不會調用驗證例程,也不會出現錯誤消息所示。

我如何獲得Cake來驗證表單?

回答

0

在您的聯繫動作中,您正在加載聯繫人模型。您必須顯式調用相關模型方法來執行驗證。如何正確閱讀manual

0

請查閱文檔以瞭解如何在控制器中插入/更新表單中的數據。你會看到類似這樣的東西:

if ($this->request->is('post')) { 
    if ($this->Contact->save($this->request->data)) { 
     // handle the success. 
    } else { 
     $this->Session->setFlash(__('The Contact could not be saved. Please, try again.')); 
    } 
}