表格元素没有显示?

时间:2011-05-15 06:59:04

标签: php zend-framework forms rendering

项目文件夹/ 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' ) );

    }
}

myproject / application / controllers / AuthenticationController.php中访问表单

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

中的

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

问题:

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

由于

2 个答案:

答案 0 :(得分:2)

它是__construct(),而不是_construct()

答案 1 :(得分:0)

这是我的完整解决方案:

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>