Solid Rhino
Solid Rhino

Reputation: 1895

Zend_Form: How to add custom HTML

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

Upvotes: 1

Views: 6084

Answers (2)

Adam
Adam

Reputation: 1108

You can add HTML to Zend_Form in a few ways. I see you've already used decorators and HtmlTag, that's good. Have you also seen DivTag, Label and my personal fave, Description?

$this->addDecorators(array(
array('ViewHelper'),
array('Errors'),
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'dd')),
array('Label', array('tag' => 'dt')),

));

Using a combination of them should allow you to add basic HTML to your form and add divs, classes and IDs. Zend Form Elements

Upvotes: 1

zerkms
zerkms

Reputation: 255115

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.

Upvotes: 3

Related Questions