How can I stop Zend form errors from being displayed as unordered lists?

Currently, error messages in my Zend form are displayed like this:

<ul class="errors"> <li>A password is required.</li> </ul> 

I do not like it. How can I do something like this:

 <div class="errors"> <p>A password is required.</p> </div> 

What I tried:

  • Removing and re-declaring the error decorator:

     $password->removeDecorator("Errors"); $password->addDecorator("Errors", array("tag" => "div")); 

    Does not work; I get the following:

     <ul tag="div" class="errors"> <li>A password is required.</li> </ul> 
  • HtmlTag other HtmlTag decorators around the Errors decorator. All this does is add style to the list. For example, I tried adding <strong></strong> tags, but instead of replacing the list, the tags wrap around it, creating a bold list.

  • Asking if the Intercal PLEASE statement is actually used:

     PLEASE $password->removeDecorator("Errors"); PLEASE $password->addDecorator("Errors", array("tag" => "div")); 

    Well, maybe I haven't tried this. But, the fact is that I can not come up with anything else to try :)

+4
source share
1 answer

The first problem is that by default Errors decorator expects an array of errors to work; You may have several validators attached to this element. This is why an unordered list works very well for this situation.

However, your desired conclusion offers one message for each element, so the question will be: where do you want this message to appear? Do you want to indicate this single message regardless of the messages created by the validators, or you want to display, say, the first message from all error messages generated by your validators (although I assume that there will be only one validator, which, as you know, is only one message you want)?

In any case, I see a custom decorator. In each case, the render() method checks for errors on the element and if there are errors, you "capture the error message" and visualize the markup you want.

In the first case - you want to specify one message, regardless of the messages created by the validators themselves - your custom decorator can accept one message "message".

In the latter case, you simply take the first error message.

As always, when writing custom decorators, you probably put them in your own namespace and then register that namespace with an element.

Alternatively, I might misunderstand your intention. You may be open to have multiple <p> tags. In this case, you can use the standard Errors decoder, but you need to call it a user assistant of the form FormErrors , which extends the standard FormErrors view-helper, which overrides the protected members as follows:

 protected $_htmlElementEnd = '</p></div>'; protected $_htmlElementStart = '<div%s><p>'; protected $_htmlElementSeparator = '</p><p>'; 

As always, you would probably put your own view helper in your own pseudo-space and register that namespace with the view object.

Nothing is completely written or verified, just some thoughts / ideas. Hope this helps!

+9
source

All Articles