2011-05-15 106 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>