3

我创建登录表单并添加 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));
}

}
4

2 回答 2

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;
}
于 2012-08-09T22:53:18.857 回答
1

您正在寻找渲染功能:

<?php
    echo $this->loginForm->render();
?>
于 2012-08-09T22:56:41.647 回答