问题
Created Form in projectfolder/application/forms/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' ) );
}
}
Accessing Form in myproject/application/controllers/AuthenticationController.php
public function loginAction() {
$this->view->heading = 'Login';
$this->view->form = new Form_Login();
}
in login.phtml
<h1><?= $this->heading; ?></h1>
<?= $this->form; ?>
Problem:
Heading is shown but not any form element is shown. What am I doing wrong here ?
Thanks
回答1:
It's __construct()
, not _construct()
.
回答2:
Here is my complete solution:
Form Class in 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
login-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>
来源:https://stackoverflow.com/questions/6006982/form-elements-are-not-shown