1

我最近正在使用zend。我发现了这个 ViewScript 表单装饰器,我发现它是使用经典 Zend 表单装饰器的最佳选择。但我在显示表格时遇到问题。我得到了代码,但没有从视图中得到显示。

这是我的代码:

形式:

class Application_Form_Registration extends Zend_Form
{
    public function init()
    {   
        $username = new Zend_Form_Element_Text("username");
        $submit = new Zend_Form_Element_Submit("submit");
        $this->setAction("/test.php");
        $this->setMethod("get");
        $this->addElements(array($username, $submit));
        $this->setElementDecorators(array(
          array('ViewScript', array(
            'viewScript'=>'test.phtml'
          ))
        ));
    }
}

控制器:

class IndexController extends Zend_Controller_Action
{
    public function init()
    {
    }

    public function indexAction()
    {
        $form = new Application_Form_Registration();
        $this->view->form = $form;

    }
}

test.phtml(我的 ViewScript)

<form action="<?php $this->escape($this->form->getAction()); ?>">
<div style="width: 100px; height: 100px; background: blue;">
    <?php echo $this->element->username; ?>
    <?php echo $this->element->submit; ?>
</div>
</form>

我的观点(index.phtml)

<?php echo $this->form; ?>

我错过了什么和/或上面的代码出错了吗?

4

2 回答 2

3

代替

  $this->setElementDecorators(array(
              array('ViewScript', array(
                'viewScript'=>'test.phtml'
              ))
            ));

$this->setDecorators(array(
              array('ViewScript', array(
                'viewScript'=>'test.phtml'
              ))
            ));

您已经基本覆盖了默认装饰器“ViewHelper”,因此没有什么可显示的。

表单(html 表单标签)和表单元素(输入类型文本、收音机等)都使用装饰器来显示自己。通过在 Zend_Form 实例上调用 setElementDecorators ,您将覆盖表单元素装饰器而不是表单装饰器,因为我们需要使用 setDecorators 代替。

于 2012-04-01T10:56:37.263 回答
1

信不信由你使用 element->getAction 在部分中访问 getAction,不要忘记回显它:

//test.php
<form action="<?php echo $this->escape($this->element->getAction()); ?>">
<div style="width: 100px; height: 100px; background: blue;">
    <?php echo $this->element->username->render(); ?>
    <?php echo $this->element->submit->render(); ?>
</div>
</form>

观点是:

//index.phtml
<?php echo $this->form ?>
于 2012-04-01T10:41:49.767 回答