无法在ViewScript中呈现Zend Form元素

时间:2011-01-15 16:24:04

标签: php zend-framework zend-form

我有这样的Login.php表单:

class Form_Login extends Zend_Form {

    public function init() {
    }

    public function __construct(){

        // Set method
        $this->setMethod('post');

        $elements = array();

        $element = $this->createElement('text','username');
        $element->setRequired(true);
        $elements[] = $element;

        $element = $this->createElement('password','password');
        $element->setRequired(true);
        $elements[] = $element;

        $this->addElements( $elements );

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

/authentication/login-form.phtml

<table>
    <tr>
        <td><?= $this->getElement('username') ?></td>
    </tr>
    <tr>
        <td><?= $this->getElement('password') ?></td>
    </tr>

</table>

当我渲染表单时,我得到以下异常:

Message: Plugin by name 'GetElement' was not found in the registry; used paths: Zend_View_Helper_: Zend/View/Helper/;C:/wamp/www/databox/application/views\helpers/ 

我哪里出错了。我应该在application.ini或Bootstrap中输入任何信息。

由于

2 个答案:

答案 0 :(得分:3)

您需要将表单传递给视图脚本才能使其正常工作。

public function someAction()
{
    $this->view->form = new Form_Login();
}

在您的视图脚本中,您可以通过

接收元素
<table>
<tr>
    <td><?= $this->form->getElement('username'); ?></td>
</tr>
<tr>
    <td><?= $this->form->getElement('password'); ?></td>
</tr>
</table>

答案 1 :(得分:1)

$this->setDecorators( array( array( 'viewScript', array( 'viewScript' =>'/authentication/login-form.phtml','form'=>$form ) ) ) );

然后在视图中

$this->form->getElement('elementName');