如何通过Zend_Form渲染结果包装一些文本

时间:2011-02-03 10:56:12

标签: php zend-framework zend-form

我需要用Zend_Form渲染的结果包装我的文本:

你将能够通过代码理解所有(因为,我的英语不好)(这个样本不起作用):

$HTML = $Form -> render( new Zend_View('asdasd') );

我应该得到:

<form>
asdasd
</form>

2 个答案:

答案 0 :(得分:1)

我认为您正在尝试做的事情(如果我理解正确的话)可以通过编写自定义表单视图助手来实现。出于这个原因,我准备了一份如何完成的草案。不确定,如果这是最好的方法,但它应该有效。

首先,让我们创建自定义表单视图助手。默认值为Zend_View_Helper_Form。 Zend_Form使用Zend_Form_Decorator_Form进行渲染,而后者又使用Zend_View_Helper_Form来实际生成表单的xhtml表示。所以我们可以创建自己的表单助手,例如名为My_View_Helper_CustomForm的{​​{1}}扩展Zend_View_Helper_Form如下:

 // Example file location: APPLICATION_PATH/views/helpers/CustomForm.php
 class My_View_Helper_CustomForm extends Zend_View_Helper_Form {


     /**
     * Render HTML form
     *
     * @param  string $name Form name
     * @param  null|array $attribs HTML form attributes
     * @param  false|string $content Form content
     * @return string
     */
    public function customForm($name, $attribs = null, $content = false)
    {

        // THE FOLLOWING FEW LINES ARE NEW.
        // get myText and unset it from $attribs afterwards.        
        $myText = '';        
        if (array_key_exists('myText', $attribs)) {
            $myText = $attribs['myText'];
            unset($attribs['myText']);
        }

        // this is from orginal form view helper
        $info = $this->_getInfo($name, $content, $attribs);
        extract($info);

        if (!empty($id)) {
            $id = ' id="' . $this->view->escape($id) . '"';
        } else {
            $id = '';
        }

        if (array_key_exists('id', $attribs) && empty($attribs['id'])) {
            unset($attribs['id']);
        }

        $xhtml = '<form'
               . $id
               . $this->_htmlAttribs($attribs)
               . '>';


        // THE NEXT LINE IS AGAIN NEW        
        // WE PUT $myText after opening <form> tag (as in your example).
        $xhtml .= $myText . 'sadfsf';



        // the rest of the form, as usuall.
        if (false !== $content) {
            $xhtml .= $content
                   .  '</form>';
        }

        return $xhtml;
    }

}

在Bootsrap.php的某个地方,你应该将视图助手本地化添加到自动加载器。也许你也可以通过向Zend_View添加视图助手目录来实现。尽管如此,我只是使用自动加载器进行如下操作:

    $autoLoader = Zend_Loader_Autoloader::getInstance();

    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
                'basePath' => APPLICATION_PATH,
                'namespace' => '',
            ));

    $resourceLoader->addResourceType('view', 'views/helpers/', 'My_View_Helper_');
    $autoLoader->pushAutoloader($resourceLoader);

完成后,我将在testAction中演示将使用上述customForm的示例代码:

    $myForm = new My_Form_SomeForm();

    // set your text you want to put in the form
    // Just don't forget to unset it the new view helper.
    $myForm->setAttrib('myText', 'asdasd'); 


    // get form decorator (i.e. Zend_Form_Decorator_Form)
    $formDecorator = $myForm->getDecorator('form');

    // $formDecorator uses Form helper to actually cunstruct xhtml of the form.
    // So we set our new helper (i.e. My_View_Helper_CustomForm)
    //  instead of default one (i.e. Zend_View_Helper_Form).
    // setHelper() method takes only string, thus ZF will need to know how to find
    // the new view helper. This was the reason I added it to the autoloader.
    $formDecorator->setHelper('customForm');

    $HTML = $myForm->render();

希望这对您有用,并帮助您解决问题。

答案 1 :(得分:0)

您可以像这样轻松地从控制器渲染表单

在控制器

$form = new Zend_Form();
//Create your form elements

$this->view->form = $form

然后在您的控制器中

echo $this->form

Zend_Form对象有一个__toString()方法,只需使用echo即可为您呈现表单。