表单元素未显示?

发布于 2024-11-07 11:29:37 字数 1101 浏览 0 评论 0原文

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' ) );

    }
}

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; ?>

< strong>问题:

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-11-14 11:29:37

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

It's __construct(), not _construct().

停滞 2024-11-14 11:29:37

这是我的完整解决方案:

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文