2013-01-01 219 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

谢谢你的帮助! –