Zend framework 带有xthml脚本的Zend_表单(不使用decorator)

Zend framework 带有xthml脚本的Zend_表单(不使用decorator),zend-framework,zend-form,Zend Framework,Zend Form,(我按照下面的步骤了解了如何在不使用decorator的情况下使用Zend_表单。) 在动作中,我创建Zend_Form对象,并在渲染前将该表单指定给视图: public function indexAction() { $form = new Zend_Form(); $form->setAction('process') ->setMethod('post'); $username = $form->

(我按照下面的步骤了解了如何在不使用decorator的情况下使用Zend_表单。)

在动作中,我创建Zend_Form对象,并在渲染前将该表单指定给视图:

public function indexAction()
{
        $form = new Zend_Form();
        $form->setAction('process')
            ->setMethod('post');

        $username = $form->createElement('text','username');
        $username->addValidator('alnum')
        ->setRequired(true)
        ->addFilter('StringtoLower');

        $description = $form->createElement('textarea','description');
        $description->addValidator('alnum')
                    ->setRequired(false)
                    ->addFilter('StringtoLower');

        $submit = $form->createElement('submit','Submit');

        $form->addElement($username);
        $form->addElement($description);
        $form->addElement($submit);

        $this->view->form = $form;
        $this->view->render('myForm/index.phtml'); //myForm is the actionController

    }
然后我将以下代码添加到它的视图脚本(index.phtml)


如何在不使用decorators的情况下解决此问题

您实际上需要删除默认的Decorator,并添加ViewScript Decorator(可能还有错误Decorator),然后对其进行渲染,请参阅本文

简单的例子:

class Demo_Form extends Zend_Form {
public function init() {
    $this->addElement('text', 'username', array( 'decorators' => array( 'ViewHeper', 'Errors' ) ));
    $this->addElement('text', 'lastname', array( 'decorators' => array( 'ViewHeper', 'Errors' ) ));
    $this->setDecorators(array( array('ViewScript' => array( 'script' => 'demoForm.phtml'))));
}
}
并使用视图脚本(demoForm.phtml):


您实际上需要删除默认的装饰器,并添加ViewScript装饰器(可能还有错误装饰器),然后对其进行渲染,请参阅本文

简单的例子:

class Demo_Form extends Zend_Form {
public function init() {
    $this->addElement('text', 'username', array( 'decorators' => array( 'ViewHeper', 'Errors' ) ));
    $this->addElement('text', 'lastname', array( 'decorators' => array( 'ViewHeper', 'Errors' ) ));
    $this->setDecorators(array( array('ViewScript' => array( 'script' => 'demoForm.phtml'))));
}
}
并使用视图脚本(demoForm.phtml):


函数索引(){

$form=new Zend_form()

。。。。。 $this->view->form=$form

}

完成此步骤后,只需转到path/to/index.phtml 及


形式;

希望这对你有用,我也是这么做的


函数索引(){

$form=new Zend_form()

。。。。。 $this->view->form=$form

}

完成此步骤后,只需转到path/to/index.phtml 及


形式;


希望这对你有用,我也是这么做的

自1.7版以来,您可以呈现单个表单元素装饰器:

echo $form->forename->renderLabel();

我认为您的代码试图找到一个名为element的装饰器,但没有成功

可以按如下方式渲染“元素”:

echo $form->forename->render($view);
话虽如此,我认为这不是你想要的。因为您将获得默认的附加装饰器

你可能想要:

echo $form->forename->renderViewHelper()

从1.7版开始,您可以呈现单个表单元素装饰器:

echo $form->forename->renderLabel();

我认为您的代码试图找到一个名为element的装饰器,但没有成功

可以按如下方式渲染“元素”:

echo $form->forename->render($view);
话虽如此,我认为这不是你想要的。因为您将获得默认的附加装饰器

你可能想要:

echo $form->forename->renderViewHelper()

正确的。当您不设置ViewScript装饰器时,它希望您将表单呈现为一个,而不是单个元素。我不想使用装饰器,它的使用太复杂了。使用这种方法时,有人知道这个错误吗。“消息:Decorator by name元素不存在”。我在HTML表单中使用了相同的视图脚本(用于控制器)。是否会导致任何错误?谢谢你的回答,这个主题有拼写错误。它必须是“Zend_Form with xhtml脚本”。很抱歉,你将只使用两个装饰器,如果你看我发布的文章,你只需要设置脚本装饰器来呈现你的表单(以及显示错误的错误)。我不明白你到底在说什么,我读过Zend_Form的Decorators文章。但我的实际表单比这更复杂,它在页面的许多地方有4个表。所以我想在视图中呈现单个表单元素。谢谢。对。当您不设置ViewScript装饰器时,它希望您将表单呈现为一个,而不是单个元素。我不想使用装饰器,它的使用太复杂了。使用这种方法时,有人知道这个错误吗。“消息:Decorator by name元素不存在”。我在HTML表单中使用了相同的视图脚本(用于控制器)。是否会导致任何错误?谢谢你的回答,这个主题有拼写错误。它必须是“Zend_Form with xhtml脚本”。很抱歉,你将只使用两个装饰器,如果你看我发布的文章,你只需要设置脚本装饰器来呈现你的表单(以及显示错误的错误)。我不明白你到底在说什么,我读过Zend_Form的Decorators文章。但我的实际表单比这更复杂,它在页面的许多地方有4个表。所以我想在视图中呈现单个表单元素。谢谢。