2009-07-30 82 views
0

我正嘗試使用CakePHP的驗證函數爲我的註冊/登錄頁面。如何獲取內置的驗證錯誤消息CakePHP顯示?

<?php 
class User extends AppModel 
{ 
    var $name = 'User'; 
    var $validate = array(
          'name' => VALID_NOT_EMPTY, 
          'password' => VALID_NOT_EMPTY, 
          'email_id' => VALID_EMAIL 
         ); 
} 

我沒有用於註冊或登錄的單獨視圖文件。我有一個主控制器中的應用程序的註冊和登錄代碼,以及單個index.ctp文件中的視圖。如果註冊或登錄有效,則頁面被重定向到主控制器的主頁。

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

    function register() 
    { 
    if (!empty($this->data)) 
     { 
     if ($this->User->save($this->data)) 
     { 
      $this->Session->setFlash('Your registration information was accepted.'); 
      $this->redirect('/main/home'); 
     } 

     } 
    } 
} 

Index.ctp

<p>Please fill out the form below to register an account.</p> 
<?php 
     echo $form->create('User', array('action' => 'register')); 
     echo $form->input('name'); 
     echo $form->input('email_id'); 
     echo $form->input('password'); 
     echo $form->end('Register'); 
?> 

<h3>Login</h3> 
<?php 
     echo $form->create('User',array('action'=>'login')); 
     echo $form->input('email_id'); 
     echo $form->input('password'); 
     echo $form->end('Login'); 
?> 

請問這是爲什麼,不顯示自定義錯誤消息。因爲,如果我有一個單獨的註冊模塊視圖文件,那麼我會得到自定義消息。

但我不想單獨的註冊視圖文件和單獨的登錄視圖文件。我想要在主控制器的索引文件中具有這兩個功能。你可以幫幫我嗎?

編輯1

如果我使用渲染,這是我在瀏覽器中得到。

您的註冊失敗。

未找到

錯誤:請求的地址「/用戶/註冊」此服務器上找到。

這是主控制器的寄存器功能:

function register() 
    { 
     if (!empty($this->data)) 
     { 
     if ($this->User->save($this->data)) 
     { 
      $this->Session->setFlash('Your registration information was accepted.'); 
      $this->render('home'); 
     } 
     else 
     { 
      $this->Session->setFlash('Your registration failed.'); 
      $this->render('index'); 
     } 

     } 
    } 

回答

0

可能有事情,因爲你正在創建一個使用同一模型兩種形式的衝突。如果您註銷登錄表單,那麼會顯示錯誤消息嗎?

+0

只有在views/users文件夾中有單獨的register.ctp文件時,纔會顯示錯誤消息。如果我將控件重定向到主頁面,當註冊失敗時,我會得到Session setFlash消息,但不會顯示錯誤消息。只有在錯誤消息顯示的註冊操作有單獨的視圖文件時。 – Angeline 2009-07-31 04:42:58

相關問題