Zend Framework 自定义表单与 viewScript [英] Zend Framework Custom Forms with viewScript

查看:23
本文介绍了Zend Framework 自定义表单与 viewScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

applications/forms/One/Nametest.php

class Application_Form_One_Nametest 扩展 Zend_Form {公共函数初始化(){$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->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>请向我们提供以下信息,以便我们了解更多你.</p><?echo $this->element->name ?><?echo $this->element->submit ?></表单>

application/controllers/IndexController.php

公共函数 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

Formtest

<?echo $this->form;?>

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

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

解决方案

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

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

applications/forms/Form.php

class Application_Form_Form 扩展 Zend_Form {公共函数 loadDefaultDecorators() {$this->setDecorators(大批(大批('视图脚本',大批('viewScript' =>'索引/formlayout.phtml',))));}公共函数初始化(){$this->setAction('/action');$this->setMethod('post');$this->addElement('text', 'name', array('装饰器' =>数组('视图助手','错误')));}}

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;?>

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

application/views/scripts/index/index.phtml

<?php echo $this ->形式;?>

application/controllers/IndexController.php

公共函数 indexAction() {$form = new Application_Form_Form();$this ->查看 ->形式= $形式;}

I am having some problems working out how to use custom forms in Zend Framework.

I have followed various guides but none seem to work. Nothing at all gets rendered.

Here is the bits of code that I am trying to use (All code below is in the default module). I have simplified the code to a single input for the test.

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>

application/controllers/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;       
?>

The above code does not throw any errors or render any part of formlayout.phtml including the form tags or text between the p tags.

Can anybody tell me what I might be doing wrong?

解决方案

I think the problem is your form element's decorator. You should set the decorator to ViewHelper and Error only. It works for me at least.

Here is the code I used and it should work

applications/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>

application/views/scripts/index/index.phtml

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

application/controllers/IndexController.php

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

这篇关于Zend Framework 自定义表单与 viewScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆