2011-08-30 39 views
1

我想爲我的web應用程序創建一個登錄表單。
即使我使用$validate陣列,也不會顯示錶單驗證errros。CakePHP不顯示我的表單錯誤

user.php的

public $validate = array(
    'email' => array(
     'notEmpty' => array(
      'rule' => 'notEmpty', 
      'message' => 'notEmpty', 
      'required' => true 
     ), 
     'isEmail' => array(
      'rule' => 'email' 
     ), 
     'isUnique' => array(
      'rule' => 'isUnique' 
     )   
    ), 
    'password' => array(
     'notEmpty' => array(
      'rule' => 'notEmpty' 
     ), 
     'minLength' => array(
      'rule' => array('minLength', 8) 
     ) 
    ) 
); 

我看不到我的用戶模型中的錯誤,所以我會告訴你我的控制器和我的觀點。

users_controller.php中

class UsersController extends AppController { 
     public $name = 'Users'; 
     public $helpers = array(
     'Form' 
    ); 


public function login() { 
    if(!empty($this->data)) { 
     if ($this->Auth->user() != null) { 
      $this->Session->setFlash('You are now logged in.', 'flash/success'); 
      $this->redirect('/'); 
     } else { 
      $this->Session->setFlash('You could not get logged in. Please see errors below.', 'flash/error'); 
     } 
    } 
} 

login.ctp

echo $this->Form->create('User', array('action' => 'login')); 
echo $this->Form->input('User.email', array(
    'label' => __('email address:', true), 
    'error' => array(
     'notEmpty' => __('Email address must not be blank.', true), 
     'isEmail' => __('Email address must be valid.', true), 
    ) 
)); 
echo $this->Form->input('User.password', array('label' => __('password:', true))); 
echo $this->Form->end('Log in'); 

我希望你能幫助我。幾小時後我找不到我的錯誤。是否有可能需要包含組件或助手?

回答

1

echo $this->Session->flash('auth');form->create之前。您不必驗證登錄表單,Auth將爲您處理此問題。閱讀食譜:http://book.cakephp.org/view/1250/Authentication

由於您使用的是Auth,所以對密碼的minLength驗證是無用的。

+0

好的,謝謝!是否可以停用登錄表單的這些必需符號? – mhmpl

+0

需要什麼符號? –

+0

使用默認佈局和css文件,CakePHP添加紅色星號(*)符號。 – mhmpl

1

驗證不會自動發生,除非您保存到數據庫中。在控制器中的登錄方法的第一行更改爲

if(!empty($this->data) && $this->User->validates()) { 
    ... 
+0

這並沒有改變任何東西。我可能需要將驗證錯誤發送到表單嗎? – mhmpl