How to remove default <b></b> labels from Zend FormErrors

139 Views Asked by At

I'm fairly new the Zend Framework and I'm stuck.

The following code is a snippet of my form's decorator setting where I'm experiencing a problem:

// prepend errors to top
$this->setDecorators(
    array(
        'FormElements',
        'Form',
        array(
            'FormErrors',
             array(
                'placement' => Zend_Form_Decorator_Abstract::PREPEND
             )
         )
    )
); 

When errors are rendered on the view I get the following:

    <ul class="form-errors">
       <li>
          <b>First Name: </b>
          <ul class="errors">
             <li>You forgot to enter your First Name.</li>
          </ul>
       </li>
    </ul>

How do you remove all of the html including the label <b>First Name: </b>?

2

There are 2 best solutions below

2
On

Just create custom decorator, try something like this

protected $_errorDecorator = array(
    'markupElementLabelEnd'   => '',
    'markupElementLabelStart' => '',
    'placement'=>Zend_Form_Decorator_Abstract::PREPEND
);

$this->setDecorators(array('FormElements','Form',
    array('FormErrors',
    $_errorDecorator)));
0
On

I realize I'm a little late to the party but since this is top 5 Google result, and still unanswered, I thought I'd contribute.

Best way is to extend Zend decorator, overload renderLabel method, add renderLabels config option and then just check if it's set. If so, don't call parent function which renders your label.

$form->setDecorators(
    array(
        'FormElements',
        'Form',
        array(
            'FormErrors',
             array(
                'placement' => 'prepend',
                'renderLabels' => false
             )
         )
    )
);

class My_Form_Decorator_FormErrors extends Zend_Form_Decorator_FormErrors
{
    /**
     * @see Zend_Form_Decorator_FormErrors::renderLabel()
     */
    public function renderLabel(Zend_Form_Element $element, Zend_View_Interface $view)
    {
        if ($this->getOption('renderLabels') === false) {
            return;
        }

        return parent::renderLabel($element, $view);
    }
}