Zend Framework: Insert DIV and Image in my form

前端 未结 8 1873
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 22:30

I need to insert html code like this in my zend form:

8条回答
  •  自闭症患者
    2020-12-19 22:50

    I created a custom element called "html". I added Html.php to /Form/Element:

    
    /** Zend_Form_Element_Xhtml */
    require_once 'Zend/Form/Element/Xhtml.php';
    
    /**
     * HTML form element
     * 
     */
    class Zend_Form_Element_Html extends Zend_Form_Element_Xhtml
    {
        /**
         * Default form view helper to use for rendering
         * @var string
         */
        public $helper = 'formHtml';
    }
    
    

    Second I had to add a view helper FormHtml.php (I put it in application/views/helpers):

    
    /**
     * Abstract class for extension
     */
    require_once 'Zend/View/Helper/FormElement.php';
    
    /**
     * Helper to show HTML
     *
     */
    class Zend_View_Helper_FormHtml extends Zend_View_Helper_FormElement
    {
        /**
         * Helper to show a html in a form
         *
         * @param string|array $name If a string, the element name.  If an
         * array, all other parameters are ignored, and the array elements
         * are extracted in place of added parameters.
         *
         * @param mixed $value The element value.
         *
         * @param array $attribs Attributes for the element tag.
         *
         * @return string The element XHTML.
         */
        public function formHtml($name, $value = null, $attribs = null)
        {
            $info = $this->_getInfo($name, $value, $attribs);
            extract($info); // name, value, attribs, options, listsep, disable
    
            // Render the button.
            $xhtml = 'view->escape($id) . '">'
                . $this->_htmlAttribs($attribs)
                . $this->view->escape($value) . '';
    
            return $xhtml;
        }
    }
    
    

    You can then add html to your form as follows:

    
    $form->createElement('html', 'someid', array('value'=>'gna

    foobar

    '));

    There might be some more simplifications possible.

提交回复
热议问题