Zend Framework:在布局中添加表单

时间:2012-08-09 22:11:16

标签: zend-framework zend-form

我创建登录表单并添加UserController-> loginAction:

 public function loginAction() {
    $form = new Application_Form_Login();

    $this->view->loginForm = $form;
}

如何在layouts / scripts / header.phtml中添加我的表单,因为我尝试了这个,但没有工作:

<?php echo $this->loginForm ?>

如果我在views / scripts / user / login.phtml中回显表单,我会看到登录表单。

这是我的登录表单:

class Application_Form_Login extends Zend_Form {

public function init() {
    $this->setDecorators(array('FormElements', 'Form'))
            ->setAction("/user/login/");

    $username = new Zend_Form_Element_Text(array('name' => 'username', 'class' => 'input-text'));
    $username->setRequired(true)
            ->setDecorators(array('ViewHelper',));

    $this->addElements(array($username));
}

}

2 个答案:

答案 0 :(得分:1)

由于布局与视图对象是分开的,因此您需要将表单分配给布局。

在您的控制器中:

public function loginAction()
{
    $form = new Application_Form_Login();

    // assign the form to the layout
    $this->_helper->layout()->loginForm = $form;
}

在你的布局中:

<?php if ($this->layout()->loginForm): ?>
    <?php echo $this->layout()->loginForm; ?>
<?php endif; ?>

请改为尝试:

// in controller:
$form = new Application_Form_Login();
$this->view->placeholder('loginForm')->set($form);

---------------

// in layout script:
$form = $this->placeholder('loginForm');
if ($form instanceof Zend_Form) {
    echo $form;
}

答案 1 :(得分:1)

您正在寻找渲染功能:

<?php
    echo $this->loginForm->render();
?>