2011-05-15 130 views
1

projectfolder /应用/表格创建的表单/ login.php中表单元素未显示?

class Form_Login extends Zend_Form { 

    public function _construct() { 

     $this->setMethod('post'); 

     $elements = array(); 

     $element = $this->addElement('text', 'username'); 
     $element->setLabel('Username'); 
     $elements[] = $element; 

     $element = $this->addElement('password', 'password'); 
     $element->setLabel('Password'); 
     $elements[] = $element; 

     $this->addElements($elements); 

     $this->setElementDecorators(array('ViewHelper')); 

    } 
} 

的myproject /应用/控制器访问表单/ AuthenticationController.php

public function loginAction() { 
    $this->view->heading = 'Login'; 
    $this->view->form = new Form_Login(); 
} 

login.phtml

<h1><?= $this->heading; ?></h1> 
<?= $this->form; ?> 

问题:

显示标题但不显示任何表单元素。我在这里做错了什么?

感谢

回答

2

__construct(),不_construct()

+0

是的,你是正确的,但现在它继续并生成另一个错误:'致命错误:未捕获异常'Zend_Controller_Response_Exception'带消息'无法发送标题S;头文件已经发送到/var/www/student/application/Bootstrap.php,第28行在/var/www/student/library/Zend/Controller/Response/Abstract.php:321' – Student 2011-05-15 07:31:01

+0

好的。我在** Bootstrap.php **中删除了'?>'(php结束标记)后的多余空格,并且删除了上面的错误。现在错误是'消息:方法setLabel不存在'.... – Student 2011-05-15 07:38:36

+0

使用$ this-> addElement('text','username',array('label'=>'Username')); – 2011-05-15 07:41:06

0

这里是我的完整的解决方案:

Form类在的login.php

class Form_Login extends Zend_Form { 

    /** 
    * Constructor 
    */ 
    public function __construct($options = null) { 

     parent::__construct($options); 

     // Set the method for the display form to POST 
     $this->setMethod('post'); 

     $elements = array(); 

     $element = $this->CreateElement('text', 'username'); 
     $element->setLabel('Username'); 
     $elements[] = $element; 

     $element = $this->CreateElement('password', 'password'); 
     $element->setLabel('Password'); 
     $elements[] = $element; 

     $element = $this->CreateElement('submit', 'submit'); 
     $element->setLabel('Login'); 
     $elements[] = $element; 

     $this->addElements($elements); 

     $this->setElementDecorators(array('ViewHelper')); 

     $this->setDecorators(array(array('ViewScript', array('viewScript' => 'authentication/login-form.phtml')))); 

    } // end construct 


} // end class 

登录-form.phtml

<form action=<?= $this->element->getAction() ?> method=<?= $this->element->getMethod() ?> > 


<table> 
    <tr> 
     <td><label><?= $this->element->username->getLabel() ?></label></td> 
     <td><?= $this->element->username; ?></td> 
    </tr> 
    <tr> 
     <td><label><?= $this->element->password->getLabel() ?></label></td> 
     <td><?= $this->element->password; ?></td> 
    </tr> 
</table> 

</form>