Search code examples
zend-frameworkpluginszend-formbootstrappingzend-decorators

Change default Zend Form Decorator on global application level?


Of course, I don't want to change library/Zend. I know that I can create class My_Form which extends Zend_Form and setup custom decorator. Than, every form extends new class My_Form...

Is there any way to set Zend Form Decorator(change default decoration) in some plugin or bootstrap, without changing existing forms ??

Or, What is the best way to override default form decorator for all forms?


Solution

  • I am not sure whether my answer will be of help, but here you go. There is a way of substituting ZF decorators with your own decorators without editing the forms themselves.

    SOLUTION #1:

    The method described here. Or in short:

    Let's say you've got a form:

    class Form extends Zend_Form
    {
        function init ()
        {
            $this->addElement ('text', 'a', array (
                'label' => 'Name'
            ));
        }
    }
    

    Then have in application.ini

    resources.view.helperPath.Default_View_Helper = APPLICATION_PATH "/views/helpers"
    

    Add a new file application/views/helpers/FormText.php

    class Default_View_Helper_FormText extends Zend_Form_Decorator_Abstract
    {
        function formText ()
        {
            return 'It is I.';
        }
    }
    

    And that is it.

    SOLUTION #2:

    Let's have this abstract class:

    abstract class Application_Style
    {
        private $_object;
    
    
    
        function __construct ($object = null)
        {
            if (isset ($object))
            {
                $this->apply ($object);
            }
        }
    
    
        function apply ($object)
        {
            $this->setObject ($object);
            if ($this->filter ())
            {
                $this->onApply ();
            }
    
            return $object;
        }
    
    
        function __call ($method, $arguments)
        {
            return call_user_func_array (array ($this->getObject (), $method), $arguments);
        }
    
    
        abstract protected function onApply ();
    
    
        protected function filter ()
        {
            return true;
        }
    
    
        function setObject ($_object)
        {
            $this->_object = $_object;
        }
    
    
        function getObject ()
        {
            return $this->_object;
        }
    }
    

    And then a descendant.

    class Application_Style_AdminForm extends Application_Style
    {
        function onApply ()
        {
                $this->addElement ($submit = new Zend_Form_Element_Submit ('submit', array(
                'label' => 'Submit',
                )));
    
                $submit
                ->removeDecorator ('DtDdWrapper')
                ->addDecorator ('HtmlTag', array (
                'placement' => Zend_Form_Decorator_HtmlTag::PREPEND,
                'tag' => 'p',
                'openOnly'  => 1,
                ))
                ->addDecorator ('Custom', array ('text' => '     '))
                ;
            }
    }
    

    In the onApply() method could be anything that suits you. Adding or removing decorators, for example. Then you can call this styling upon your form like this:

    new Application_Style_AdminForm ($this);
    

    which allows you to manipulate the form representation but without changing it directly.