Group form errors using the FormErrors decorator with the Zend Framework

Saturday, January 2, 2010

This example Zend_Form class shows how to group the elements' errors above the form using the Zend_Form_Decorator_FormErrors decorator.

This combined with the decorator's following methods make it easy to display those errors in a totally different format.

  • setMarkupElementLabelEnd()
  • setMarkupElementLabelStart()
  • setMarkupListEnd()
  • setMarkupListItemEnd()
  • setMarkupListItemStart()
  • setMarkupListStart()
<?php
class Quickies_Form_FormErrors extends Zend_Form
{
    public function init()
    {
        /**
         * Add default decorators for this form (not it's elements)
         * The ones below are the same as loaded by default
         * just with FormErrors added.
         *
         * This groups the erros together
         */
        $this->setDecorators(array(
            'FormErrors',
            'FormElements',
            array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
            'Form'
        ));
        
        // add input 1
        $this->addElement('text','textinput1',array(
            'required' => true
        ));
        
        // add input 2
        $this->addElement('text','textinput2',array(
            'required' => true
        ));
        
        // add submit button
        $this->addElement('submit','submitinput1', array(
            'label'    => 'submit it'
        ));
        
        /**
         * Remove Errors decorator from all elements
         * so that they don't also display them
         */
        foreach ($this->getElements() as $element) {
            $element->removeDecorator('Errors');
        }
    }
}

Leave a reply

 
Please type this word backwards: s6cug
 
 
 

Blog categories

Zend-Framework Databases Php

About

Sreknord.net is the personal web space of software engineer, Leonard Dronkers (NL). At Sreknord.net you will find interesting information about software engineering and web-development.

Leonard Dronkers

Software engineer, Leonard Dronkers is currently working as a web-developer at VNU Media (NL).

Professional interests:

To get in touch with Leonard, please use the contact form.