【问题标题】:Zend Framework Custom Forms with viewScript带有 viewScript 的 Zend Framework 自定义表单
【发布时间】:2011-12-09 05:35:44
【问题描述】:

我在解决如何在 Zend Framework 中使用自定义表单时遇到一些问题。

我遵循了各种指南,但似乎都没有。什么都没有渲染。

这是我尝试使用的代码片段(下面的所有代码都在默认模块中)。我已将代码简化为用于测试的单个输入。

applications/forms/One/Nametest.php

class Application_Form_One_Nametest extends Zend_Form {

    public function init() {

        $this->setMethod('post');

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Box Name')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Submit Message');
        $submit->setAttrib('id', 'submitbutton');
        $submit->setAttrib('class', 'bluebutton');

        $this->addElements(array($name, $submit));
    }

}

application/views/scripts/one/formlayout.phtml

<form action="<?= $this->escape($this->form->getAction()) ?>" method="<?= $this->escape($this->form->getMethod()) ?>">

    <p>
        Please provide us the following information so we can know more about
        you.
    </p>

    <? echo $this->element->name ?>
    <? echo $this->element->submit ?>

</form>

应用程序/控制器/IndexController.php

public function formtestAction() {
    $form = new Application_Form_One_Nametest();
    $form->setDecorators(array(array('ViewScript', array('viewScript' => 'one/formlayout.phtml'))));

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

application/views/scripts/index/formtest.phtml

<h1>Formtest</h1>
<?
echo $this->form;       
?>

以上代码不会抛出任何错误或渲染formlayout.phtml的任何部分,包括表单标签或p标签之间的文本。

谁能告诉我我做错了什么?

【问题讨论】:

  • 一个常见的错误是在您的表单类中有一个不调用父构造函数的构造函数。这会导致空表单输出,因为它是最终调用您的 init() 方法的父构造函数。只是大声思考......

标签: forms zend-framework


【解决方案1】:

我认为问题在于您的表单元素的装饰器。您应该只将装饰器设置为 ViewHelper 和 Error。至少对我有用。

这是我使用的代码,它应该可以工作

应用程序/forms/Form.php

class Application_Form_Form extends Zend_Form {

public function loadDefaultDecorators() {
    $this->setDecorators(
        array(
            array(
                'ViewScript',
                array(
                    'viewScript' => 'index/formlayout.phtml',
                )
            )
        )
    );
}

    public function init() {
        $this->setAction('/action');
        $this->setMethod('post');

        $this->addElement('text', 'name', array(
            'decorators' => array('ViewHelper', 'Errors')
        ));
    }
}

application/views/scripts/index/formlayout.phtml

<form action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
  <div>
    <label for="name">Box Name</label>
    <?php echo $this->element->name; ?>
  </div>

  <input type="submit" value="Submit Message" id="submitbutton" class="bluebutton">
</form>

应用程序/视图/脚本/index/index.phtml

<!-- application/views/scripts/index/index.phtml -->
<?php echo $this -> form; ?>

应用程序/控制器/IndexController.php

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

【讨论】:

    【解决方案2】:

    这是一个非常简单的示例,可以让您从this article 改编。

    形式:-

    class Application_Form_Test extends Zend_Form
    {
        public function init()
        {
            $this->setMethod('POST');
            $this->setAction('/');
            $text = new Zend_Form_Element_Text('testText');
    
            $submit = new Zend_Form_Element_Submit('submit');
    
            $this->setDecorators(
                    array(
                        array('ViewScript', array('viewScript' => '_form_test.phtml'))
                        )
                    );
    
            $this->addElements(array($text, $submit));
            $this->setElementDecorators(array('ViewHelper'));
        }
    }
    

    setDecorators()addElements()setElementDecorators() 的调用顺序在这里非常重要。

    视图脚本_form_test.phtml 可以任意命名,但它需要在/views/scripts 中才能被渲染器找到。

    /views/scripts/_form_test.phtml 看起来像这样:-

    <form id="contact" action="<?php echo $this->element->getAction(); ?>" 
          method="<?php echo $this->element->getMethod(); ?>">
    
    <p>
    Text<br />
    <?php echo $this->element->testText; ?>
    </p>
    
    <p>
    <?php echo $this->element->submit ?>
    </p>
    
    </form>
    

    您实例化表单,将其传递给视图并照常呈现。此示例的输出如下所示:-

    <form id='contact' action='/' method='post'>
        <p>
            Text<br />
            <input type="text" name="testText" id="testText" value=""></p>
        <p>
    
        <input type="submit" name="submit" id="submit" value="submit"></p>
    </form>
    

    这应该足以让您开始创建自己的表单。

    【讨论】:

      【解决方案3】:

      通常,如果您在屏幕上看不到任何内容,则表示发生了某种错误。 也许你关闭了错误或其他东西,也许没有。我只是想给你一些想法。

      我唯一能发现的地方如下。 在下面的代码中,您仍然必须在尝试打印出元素时指定表单。

      <form>
      action="<?php $this->escape($this->element->getAction()) ?>" 
      method="<?php $this->escape($this->element->getMethod()) ?>" >
      
      <p>
          Please provide us the following information so we can know more about
          you.
      </p>
      
      <?php echo $this->element->getElement( 'name' ); ?>
      <?php echo $this->element->getElement( 'submit' ) ?>
      
      </form>
      

      正如 vascowhite 的代码所示,一旦您进入视图脚本,带有表单的变量称为元素。 viewscript 装饰器使用 partial 来进行渲染,因此它在 viewscript 中使用不同的变量名创建自己的范围。

      因此,虽然在您的原始视图中它被称为 $form,但在视图脚本中您必须将其称为元素。

      另外,也许这是复制/粘贴的匆忙,但您使用了&lt;? ?&gt; 标签而不是&lt;?= ?&gt;&lt;?php ?&gt; 标签。也许这导致了一些超出解析范围的错误,这就是你没有输出的原因。

      【讨论】:

      • 我花了一段时间才弄清楚我必须使用 <为了让“
      • 只需缩进 4 个空格以显示代码并使用反引号转义内联或 cmets 中的字符
      猜你喜欢
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      相关资源
      最近更新 更多