窗体不渲染

问题描述:

我正在使用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,所以在这下面的在线咨询这是我最好的尝试,教程等

您无意中创造没有元素的形式,这就是为什么没有出现。在你的表单对象的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)); 
    } 
} 

它应该工作。

试着做这样的事情,而不是:

http://framework.zend.com/manual/en/zend.form.forms.html

+0

我会upvote这2次,如果我可以 – Adi 2012-07-10 18:29:19