Php – Zend_Form: How to add custom HTML

PHPzend-formzend-framework

I 'am bussy creating a form with zend_form to add a page to a website. This form is for a CMS.

This is the code:

public function init()
{
        // display errors on top
        $this->setDecorators(array(
            array('FormErrors', array('markupElementLabelEnd' =>'', 'markupElementLabelStart' =>'')),
            'FormElements',
            array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
            'Form'
        ));

        // Set standard properties
        $this->setName('Admin_Form_Addpage')
             ->setAction("");


        // Textbox for the page title. The textbox is required
        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Title')
              ->setAttrib('placeholder', 'Title')
              ->setAttrib('autofocus', "")
              ->setRequired();

        // WYSIWYG editor
        $wysiwyg = new Zend_Form_Element_Textarea('pagecontent');
        $wysiwyg->setLabel('Content')
                ->setRequired();

        // Advanced CSS
        $css = new Zend_Form_Element_Textarea('css');
        $css->setLabel('CSS')
            ->setAttrib('rows', 10)
            ->setAttrib('cols', 60);

        // Advanced JS
        $js = new Zend_Form_Element_Textarea('js');
        $js->setLabel('Java Script')
           ->setAttrib('rows', 10)
           ->setAttrib('cols', 60);

        // Build form
        $this->addElement($title)
             ->addElement($wysiwyg)


             // Add submit button
             ->addElement('submit', 'add', array('label' => 'Add'))
             ->addElement($css)
             ->addElement($js)
             ->addDisplayGroup(array('css','js'), 'advanced',array('disableLoadDefaultDecorators' => true));

        $advanced = $this->getDisplayGroup('advanced');
        $advanced->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'div', 'id' => 'div_advanced'))
        ));

         /**
         * Remove Errors decorator from all elements
         * so that they don't also display them
         */
        foreach ($this->getElements() as $element) {
            $element->removeDecorator('Errors');
 }

Now I want to toggle the advanced component with jQuery. But to for this to work I need a link.

So my question is, is there some one that knows how to do that? Or even better has some one a custom form element that can add all types of html?

Thank you,

Ivo Trompert

Best Answer

If your advanced jquery script is related to some particular element - you could decorate it with ViewScript. It allows you to apply any html to the current element.

Related Topic