2014-10-10 137 views
0

我創建了一個表格,我需要用模型和控制器。這裏是我的形式如何使用模型和控制器

index.ctp

<?php echo $this->Form->create('Contact',array('url'=>array('controller'=>'contacts','action'=>'add'))); 

echo $this->Form->text('name'); 

模型來驗證在CakePHP中檢驗表單字段:聯繫.PHP

class Contact extends AppModel 
{ 
     var $name = 'Contact'; 
     var $useTable = false; 

     public $validate = array(
     'name' => array(
      'alphaNumeric' => array(
       'rule'  => 'alphaNumeric', 
       'required' => false, 
       'message' => 'Letters and numbers only' 
      ), 
      'between' => array(
       'rule' => array('between', 5, 15), 
       'message' => 'Between 5 to 15 characters' 
      ) 
     ) 
    ); 
} 

控制器:ContactsController.php

public function add() 
    { 
     $this->Contact->validates(); 

      $this->request->data['Country']['country_name']=$this->request->data['Contact']['country']; 

      $this->Country->saveall($this->request->data); 

      $this->redirect('/Contacts/index/'); 

    } 

我試圖通過谷歌搜索做驗證,但對我來說似乎很難,所以如果有人可以描述這個過程,這將是一個很大的幫助。我的cakephp版本是2.3.8。我只需要驗證這個名稱字段,因爲當我點擊提交它會在窗體中顯示此消息。

+0

你的問題是不太清楚。另外請始終提及您的確切CakePHP版本並相應地標記您的問題!這就是說:** http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html** – ndm 2014-10-10 09:04:44

+0

$ this-> Contact-> validates() ;根據有效或無效的數據返回一個布爾值true或false,並使用$ this-> Contact-> set($ this-> request-> data);在$ this-> Contact-> validates()之前; – Abhishek 2014-10-10 09:09:35

+0

我也編輯了我的問題,當我把$ this-> Contact-> validates()提交給我顯示這個錯誤後 致命錯誤:調用成員函數validates()對/ opt/lampp中的非對象/htdocs/projects/cake/cakephp/app/Controller/ContactsController.php 74行..請讓我知道如果你需要更多的輸入從我 – Ron 2014-10-10 09:14:11

回答

1

控制器代碼應該是這樣的 驗證CakePHP中的過程就像是

1) as you have defined validation rules in CakePHP model public `$validates = array();` 

2) when ever you do a save on particular model directly or through any association 
a callback method beforeValidate for that model gets called to validate the data which is being saved. 

3) once the data is validated then beforeSave callback is called after this save method is called. 

4) we can also validate the form input fields in controller using $this->Model->validates() but then while saving we have to disable the beforeValidate callback by doing 

$this->Model->save($data,array('validate'=>false)); 

否則你將最終驗證同樣的數據兩次

控制器代碼應該是有點這樣。

public function add() { 
     // here we are checking that the request is post method 
     if ($this->request->is('post')) { 
       $this->request->data['Country']['country_name'] 
           = $this->request->data['Contact']['country']; 
       // here we are saving data 
      if ($this->Contact->saveAll($this->request->data)) { 

       //here we are setting a flash message for user 
       $this->Session->setFlash('your record has been added','success'); 

       $this->redirect(array('controller'=>'contacts','action' => 'index')); 
      } else { 
       //here we are setting a flash message for user for error if input are not   
       //validated as expected 
       $this->Session->setFlash('sorry we could add your record','error'); 
      } 

     } 

    } 

欲瞭解更多信息,可以隨時參考http://book.cakephp.org/2.0/en/models/callback-methods.html

相關問題