开发者

How can I wrap some text by Zend_Form result of render

I need wrap my text by result's of Zend_Form render:

You will be able to understand all by the code (because, my english bad) (this sample it's doesn't work):

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

I supposed to get:

开发者_运维问答<form>
asdasd
</form>


I think that what you are trying to do (if I understand you correctly), can be achieved by writing custom Form view helper. For this reason, I prepared a draft of how it could be done. Not sure, if this is the best way of doing it, but it should work.

First, lets create our custom form view helper. The default one is Zend_View_Helper_Form. Zend_Form is using Zend_Form_Decorator_Form for rendering, which in turn is using Zend_View_Helper_Form to actually produce a xhtml representation of the form. So we could create our own form helper, e.g. called My_View_Helper_CustomForm that extends Zend_View_Helper_Form as follows:

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

}

Somewhere in the Bootsrap.php you should add the view helpers localization to the autoloader. Probably you could also do it by adding view helpers directory to Zend_View. nevertheless, I just do it using autoloader as follows:

    $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);

Having done that, I'll demonstrate example code in testAction that will use the above 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();

Hope this will be useful to you and will help you to solve problem.


You can render a form from a controller easily like this

In Controller

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

$this->view->form = $form

Then in your controller

echo $this->form

The Zend_Form object has a __toString() method which will render the form for you by simply using echo.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜