2013-01-01 191 views
0

我正在嘗試構建我的第一個CakePHP應用程序,並且在嘗試添加到數據庫時沒有顯示錶單驗證時遇到了一些問題。在提交表單時,我會看到閃光消息(如下面的控制器所示),但個別驗證不​​會顯示。CakePHP未顯示錶單驗證消息

型號:

class Client extends AppModel { 

public $validate = array (

'businessName' => array (
    'mustNotBeBlank'=>array(
     'rule'=>'notEmpty', 
     'message'=>'Must not be empty' 
       ), 
    'mustBeUnique'=>array(
     'rule'=>'isUnique', 
     'message'=>'Name already registered' 
       ), 
    'maxLength50'=>array (
     'rule'=>array('maxLength', 50), 
    'message'=>'Exceeds 50 Characters' 
       ) 
    ), 

'address1' => array (
    'mustNotBeBlank'=>array(
     'rule'=>'notEmpty', 
     'message'=>'Must not be empty' 
       ), 
    'maxLength50'=>array (
     'rule'=>array('maxLength', 50), 
    'message'=>'Exceeds 50 Characters' 
       ) 
    ), 

'address2' => array (
    'mustNotBeBlank'=>array(
     'rule'=>'notEmpty', 
     'message'=>'Must not be empty' 
       ), 
    'maxLength50'=>array (
     'rule'=>array('maxLength', 50), 
    'message'=>'Exceeds 50 Characters' 
       ) 
    ), 
'address3' => array (
    'mustNotBeBlank'=>array(
     'rule'=>'notEmpty', 
     'message'=>'Must not be empty' 
       ), 
    'maxLength50'=>array (
     'rule'=>array('maxLength', 50), 
    'message'=>'Exceeds 50 Characters' 
       ) 
    ), 

'postCode' => array (
    'mustNotBeBlank'=>array(
     'rule'=>'notEmpty', 
     'message'=>'Must not be empty' 
       ), 
    'postCode' => array(
      'rule'=>array('postal', null, 'uk'), 
      'message'=>'Please enter a valid postcode' 
        ) 
    ), 

'telephone1' => array (
    'mustNotBeBlank'=>array(
     'rule'=>'notEmpty', 
     'message'=>'Must not be empty' 
       ), 
    'maxLength11'=>array (
     'rule'=>array('maxLength', 11), 
    'message'=>'Exceeds phone number length' 
       ), 
    'mustBeNumber'=>array(
      'rule' =>'numeric', 
      'message' => 'Must be a number' 
        ) 
    ), 

'telephone2' => array (
    'maxLength11'=>array (
     'rule'=>array('maxLength', 11), 
    'message'=>'Exceeds phone number length' 
       ), 
    'mustBeNumber'=>array(
      'rule' => 'numeric', 
      'message' => 'Must be a number' 
        ) 

    ), 

'email' => array (
     'rule' => array('email', true), 
     'message' => 'Please supply a valid email address.' 
), 

    'domain' => array (
     'rule' => 'url', 
     'message' => 'Please supply a valid email address.' 
) 
); 

} 

瀏覽(HTML爲簡單起見刪除):

echo $this->form->create(); 
echo $this->Form->input('businessName', array('label' => 'Business Name')); 
echo $this->Form->input('address1', array('label' => 'Address')); 
echo $this->Form->input('address2', array('label' => '')); 
echo $this->Form->input('address3', array('label' => '')); 
echo $this->Form->input('postCode', array('label' => 'Postcode')); 
echo $this->Form->input('telephone1', array('label' => 'Landline')); 
echo $this->Form->input('telephone2', array('label' => 'Mobile')); 
echo $this->Form->input('email', array('label' => 'Email')); 
echo $this->Form->input('domain', array('label' => 'Domain')); 

$options = array(
    'label' => 'Add', 
    'div' => array(
     'class' => 'btn btn-primary', 
    ) 
); 
echo $this->Form->end($options); 

添加動作控制器:

public function add() { 
    if ($this->request->is('post')) { 
     $this->Client->create(); 
     if ($this->Client->save($this->request->data)) { 
      $this->Session->setFlash('Client has been saved.'); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash('Unable to add your post.'); 
     } 
    } 
} 

再次感謝您的幫助。

+0

首先看看你的數據庫名稱。 http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html – Jelmer

回答

7

你的表單創建標籤

echo $this->form->create(); 

而應該是

echo $this->Form->create(); 

注意大寫F.剛試過在本地和小寫 - >形式沒有報告的錯誤消息。

+0

我的道歉,這是一個錯字發生的複製代碼到SO –

+3

哦,你的表單創建標籤是問題.. echo $ this-> form-> create(); 應該是 echo $ this-> Form-> create(); 在這裏嘗試過了,小寫字母無法報告錯誤。我已經更新了我的答案以反映這一點。 – Happy

+0

謝謝你的幫助! –