luca
luca

Reputation: 37146

Zend form: errors don't show up

Zend talk.I build a custom Zend_form in my web app.The problem is that I can't get the errors to show up (when I submit the form without any text).Am I missing something obvious?

class Commentform extends Zend_Form
{
 public function init()
 {  

  $this->setMethod('post');
  $this->setAction('');
  $text=new Zend_Form_Element_Textarea('text');
  $text->setRequired(true)
  ->addFilter('StringTrim')
  ->addFilter('StripTags')
  ->setDescription('bla bla');

 $submit=new Zend_Form_Element_Submit('commenta');

 $this->addElements(array($text,$submit));
 $this->setElementDecorators(array(
 'ViewHelper',
 array('Description',array(
       'tag'=>'span','class'=>'medium','placement'=>'PREPEND')),
 ));

$this->setDecorators(array(
'FormElements',
 'FormErrors',
'Form',array('Description',array('tag'=>'h2','placement'=>'prepend')),
 array('HtmlTag', array('tag' => 'div','class'=>'write_comment')),
));

$this->setDescription('zend zend');
}
}

thanks

Luca

Upvotes: 0

Views: 1684

Answers (2)

faken
faken

Reputation: 6852

You have to put an "Errors" decorator in the form elements. Zend_Form_Element loads this "Errors" decorator by default, as you can see in the Zend_Form_Element source code:

public function loadDefaultDecorators()
{
    ...
    $this->addDecorator('ViewHelper')
        ->addDecorator('Errors')
        ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
        ->addDecorator('HtmlTag', array('tag' => 'dd', 'id'  => array('callback' => $getId)))
        ->addDecorator('Label', array('tag' => 'dt'));
    ...
}

Because you are overriding this behavior without providing an "Errors" decorator, element-level errors do not show up.

Upvotes: 2

Phil
Phil

Reputation: 165059

The correct decorator to use on your form is FormErrors, eg

$this->setDecorators(array(
'FormElements',
'FormErrors',
'Form',array('Description',array('tag'=>'h2','placement'=>'prepend')),
 array('HtmlTag', array('tag' => 'div','class'=>'write_comment')),
));

The Errors decorator is for elements.

Upvotes: 2

Related Questions