如何在视图中渲染表单元素?

发布于 2024-11-07 12:09:47 字数 1373 浏览 4 评论 0原文

我创建了一个像这样的表单:

class Form_Login extends Zend_Form {

    public function __construct() {

        $this->setMethod('post');
        $elements = array();

        // username
        $element = $this->addElement('text', 'username', array('label' => 'Username') );
        $elements[] = $element;

        // password
        $element = $this->addElement('password', 'password', array('label' => 'Password'));
        $elements[] = $element;

        // submit 
        $element = $this->addElement('submit', 'submit', array('label' => 'Login'));
        $elements[] = $element;

        $this->addElements( $elements );
        $this->addDecorator('ViewHelper');
        $this->setDecorators(array(array('ViewScript', array('viewScript' => 'authentication/login-form.phtml' ))));   
    }
}

现在在 login-form.phtml 文件中,我呈现这样的元素:

<form action='submitlogin' method='post' id='loginform'>  

Login Form

<?=  $this->form->getElement('username');  ?>
<?=  $this->form->getElement('password');  ?>

</form>

它给了我以下错误:

Fatal error: Call to a member function getElement() on a non-object in
/var/www/student/application/views/scripts/authentication/login-form.phtml on line 5

如何在外部脚本中呈现元素...

I have created a form like this:

class Form_Login extends Zend_Form {

    public function __construct() {

        $this->setMethod('post');
        $elements = array();

        // username
        $element = $this->addElement('text', 'username', array('label' => 'Username') );
        $elements[] = $element;

        // password
        $element = $this->addElement('password', 'password', array('label' => 'Password'));
        $elements[] = $element;

        // submit 
        $element = $this->addElement('submit', 'submit', array('label' => 'Login'));
        $elements[] = $element;

        $this->addElements( $elements );
        $this->addDecorator('ViewHelper');
        $this->setDecorators(array(array('ViewScript', array('viewScript' => 'authentication/login-form.phtml' ))));   
    }
}

Now in login-form.phtml file I render elements like this:

<form action='submitlogin' method='post' id='loginform'>  

Login Form

<?=  $this->form->getElement('username');  ?>
<?=  $this->form->getElement('password');  ?>

</form>

It gives me following error:

Fatal error: Call to a member function getElement() on a non-object in
/var/www/student/application/views/scripts/authentication/login-form.phtml on line 5

How to render elements in external script...

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

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

发布评论

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

评论(2

或十年 2024-11-14 12:09:47

您可以像这样从视图脚本中调用元素:

<?= $this->element->username ?>

对于特定的元素组件,您可以使用以下内容:

<? $el = $this->element->username; ?>
<label><?= $el->getLabel() ?></label>
<?= $this->formText($el->getName(), $el->getValue(), $el->getAttribs()) ?>

You can call elements from the view scripts like this:

<?= $this->element->username ?>

For specific element components you can use the next things:

<? $el = $this->element->username; ?>
<label><?= $el->getLabel() ?></label>
<?= $this->formText($el->getName(), $el->getValue(), $el->getAttribs()) ?>
折戟 2024-11-14 12:09:47

这是我的完整解决方案:

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 和您的相关数据。
原文