views:

154

answers:

1

I am using Zend Framework. For a particular form, there is not enough space to show the errors next to the form elements. Instead, I want to be able to display the errors above the form. I imagine I can accomplish this by passing $form->getErrorMessages() to the view but how do I disable error messages from being shown be each element?

+2  A: 

You can add decorators to form elements using setElementDecorators. Zend_Form has a function called just after init entitled loadDefaultDecorators. In your subclass, you can override this like so:

/**
 * Load the default decorators for forms.
 */
public function loadDefaultDecorators()
{
    // -- wipe all
    $this->clearDecorators();

    // -- just add form elements
    // -- this is the default
    $this->setDecorators(array(
       'FormElements',
        array('HtmlTag', array('tag' => 'dl')),
        'Form'
    ));

    // -- form element decorators
    $this->setElementDecorators(array(
        "ViewHelper",
        array("Label"),
        array("HtmlTag", array(
            "tag"   => "div",
            "class" =>"element",
        )),
    ));

    return $this;
}

Assuming you added your elements in init, that applies those decorators to each element in the form. You'll note the absence of the "Errors" decorator in setElementDecorators. You could also try looping through the form elements and using removeDecorator to remove just the Errors decorator.

Typeoneerror