Zend:ViewScript装饰器不渲染表单元素

时间:2012-04-01 10:26:46

标签: php zend-framework view helper view-helpers

我最近刚和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; ?>

上述代码是否遗漏了某些内容和/或错误了?

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:1)

信不信由你在部分使用element-&gt; 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 ?>