Search code examples
phpzend-formdecoratorzend-decorators

overwrite specific Decorators for all Zend_Form Objects


i am working on styling a project and everybody used Zend_Form and default Elements. This makes it impossible to style submit buttons. Therefore i would like to overwrite the default Zend_Form Decorator for submit buttons, but without changing every line where a Zend_Form is created.

Is that possible? If yes, how?


Solution

  • You probably want to subclass Zend_Form_Element_Submit and use loadDefaultDecorators() to set the default decorators for your submits:

    class My_Form_Element_Submit extends Zend_Form_Element_Submit
    {
        public function loadDefaultDecorators()
        {
            // set your default decorators for the submit element       
            $decorators = $this->getDecorators();
            if (empty($decorators)) {
                $this->setDecorators(array(
                    'ViewHelper',
                    array(
                        array('field' => 'HtmlTag'),
                        array(
                            'tag'   => 'span',
                            'class' => 'some-wrapper-class'
                        ) 
                    )
                ));
            }
        }
    }
    

    The above decorators would result in HTML code looking something like this, allowing you to easily style your submit button:

    <span class="some-wrapper-class"> 
        <input type="submit" name="save" id="save" value="Save">
    </span>