带有viewScript的Zend框架自定义表单 [英] Zend Framework Custom Forms with viewScript

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

问题描述



我遵循各种指南,但似乎没有任何工作。完全没有任何东西被呈现。



这是我尝试使用的代码片段(以下所有代码都在默认模块中)。

applications / forms / One / Nametest.php



pre $ 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>
请提供以下信息,以便我们了解更多有关
的信息。
< / 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;
?>

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



有人可以告诉我我可能做错了什么?

解决方案

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



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

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($ b $'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>方框名称< / label>
<?php echo $ this-> element-> name; ?>
< / div>

< input type =submitvalue =Submit Messageid =submitbuttonclass =bluebutton>
< / form>

application / views / scripts / index / index.phtml

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

application / controllers / IndexController.php

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


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

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

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