2012-07-10 147 views
0

我正在使用zend框架,並試圖使用zend窗體,MVC和OOP輸出一個簡單的登錄窗體。窗體不渲染

我的代碼是下面: 的控制器 IndexController.php

class IndexController extends Zend_Controller_Action 
{ 

    public function init() 
    { 
     /* Initialize action controller here */ 
    } 

    public function indexAction() 
    { 
     $this->view->loginForm = $this->getLoginForm(); 
    } 

    public function getLoginForm() 
    { 
     $form = new Application_Form_Login; 
     return $form; 
    } 
} 

這是以下形式: 的login.php

class Application_Form_Login extends Zend_Form 
{ 

    public function init() 
    { 
     $form = new Zend_Form; 

     $username = new Zend_Form_Element_Text('username'); 
     $username 
      ->setLabel('Username') 
      ->setRequired(true) 
     ; 

     $password = new Zend_Form_Element_Password('password'); 
     $password 
      ->setLabel('Password') 
      ->setRequired(true) 
     ; 

     $submit = new Zend_Form_Element_Submit('submit'); 
     $submit->setLabel('Login'); 

     $form->addElements(array($username, $password, $submit)); 

    } 
} 

和視圖: index.phtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 

    </head> 

    <body> 

     <div id="header"> 
      <div id="logo"> 
       <img src="../application/images/logo.png" alt="logo"> 
      </div> 
     </div> 

     <div id="wrapper"> 
      <?php echo $this->loginForm; ?> 
     </div> 
    </body> 
</html> 

我是新來的Zend Framework,MVC和OOP,所以在這下面的在線諮詢這是我最好的嘗試,教程等

回答

5

您無意中創造沒有元素的形式,這就是爲什麼沒有出現。在你的表單對象的init方法中,你正在創建一個新的實例Zend_Form,$form然後你什麼都不做,而不是將元素添加到當前實例。改變你的班級:

class Application_Form_Login extends Zend_Form 
{ 
    public function init() 
    { 
     $username = new Zend_Form_Element_Text('username'); 
     $username 
      ->setLabel('Username') 
      ->setRequired(true) 
     ; 

     $password = new Zend_Form_Element_Password('password'); 
     $password 
      ->setLabel('Password') 
      ->setRequired(true) 
     ; 

     $submit = new Zend_Form_Element_Submit('submit'); 
     $submit->setLabel('Login'); 

     $this->addElements(array($username, $password, $submit)); 
    } 
} 

它應該工作。