views:

87

answers:

1

I am currently trying to build a simple custom layer that I will extend instead of Zend_Form. For instance, My_Form.

I want all my forms to look the same so I am setting this in My_Form. Here is what it is so far.

class My_Form extends Zend_Form
{
    protected $_elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')),
        array('Label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    );
}

And all of my forms will extend this. Now this is working, the problem comes with the $_elementDecorators array. I am wrapping the Label in a "td" and the Label Decorator is applying the default "id" to that "td" but I am wanting to add a class to that "td" as well.

Is there anyway to accomplish this, with this array? If not, is there a better way to do this? Or if so, can someone please describe to me how this array works?

Desired result:

<tr>
    <td class='label_cell'>
        <label />
    </td>
    <td class='value_cell'>
        <input />
    </td>
</tr>

Thank you.

A: 

I have found a solution, although not sure it is the best.

Here I have decided to just create a custom decorator and load that up instead.

/**
 * Overide the default, empty, array of element decorators.  
 * This allows us to apply the same look globally
 * 
 * @var array
 */
protected $_elementDecorators = array(
    'ViewHelper',
    'Errors',
    array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')),
    array('CustomLabel', array('tag' => 'td')),
    array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
);

/**
 * Prefix paths to use when creating elements
 * @var array
 */
protected $_elementPrefixPaths = array(
    'decorator' => array('My_Form_Decorator' => 'My/Form/Decorator/')
);

Decorator:

class My_Form_Decorator_CustomLabel extends Zend_Form_Decorator_Label
{
    public function render($content)
    {
        //...
        /**
         * Line 48 was added for the cutom class on the <td> that surrounds the label
         */
        if (null !== $tag) {
            require_once 'Zend/Form/Decorator/HtmlTag.php';
            $decorator = new Zend_Form_Decorator_HtmlTag();
            $decorator->setOptions(array('tag'   => $tag,
                                         'id'    => $this->getElement()->getName() . '-label',
                                         'class' => 'label_cell'));

            $label = $decorator->render($label);
        }
        //...
    }
}

Although this works just fine, I am still curious if there is a simpler way to do this.

Any ideas?

KSolo