使用viewScript的Zend Framework自定义表单

som*_*ser 7 forms zend-framework

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

我遵循了各种指南,但似乎都没有.什么都没有呈现.

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

应用/表格/一/ 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));
    }

}
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/脚本/一个/ 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>
Run Code Online (Sandbox Code Playgroud)

应用/控制器/ 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;
}
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/脚本/指数/ formtest.phtml

<h1>Formtest</h1>
<?
echo $this->form;       
?>
Run Code Online (Sandbox Code Playgroud)

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

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

vas*_*ite 1

这是一个非常简单的示例,可帮助您适应本文

表格:-

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'));
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

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