Search code examples
phpzend-frameworkzend-formdecoratorzend-decorators

Zend_Form decorators: Unable to view error messages (validation)


When we add decorators to the form elements in zend, the validation message doesn't shows why ?

Code Example :

$this->addElement('Text', 'Last_Name',array(
        //'decorators' => $this->elementDecoratorsTr,
        'label' => 'Last Name:',
        'required' => false,
        'filters' => array('StringTrim'),
            'validators' => array(array('validator' => 'StringLength','validator' => 'Alpha')) 
        ));

Solution

  • Here is Zend_Form_Element source code:

    $decorators = $this->getDecorators();
    if (empty($decorators)) {
        $this->addDecorator('ViewHelper')
            ->addDecorator('Errors')   // notice Errors decorator
            ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
            ->addDecorator('HtmlTag', array('tag' => 'dd', 
                                            'id'  => $this->getName() . '-element'))
            ->addDecorator('Label', array('tag' => 'dt'));
    }
    

    If you set your own decorators then the default ones are not loaded.

    In order to see validation messages you need to have an Errors decorator among the decorators you set.