Form elements are not shown?

自闭症网瘾萝莉.ら 提交于 2019-12-11 12:39:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!