After looking into this a bit further, I found that the "errors" decorator calls Zend_Form_Element#getMessages(), which simply returns the $_messagesarray. Unfortunately, there's no way to clear that array without calling Zend_Form_Element#isValid(). When calling Zend_Form_Element#setErrors(), the provided errors are appended to the $_messages array. In other words, once an error message has been added to the stack during validation, it's practically impossible to remove.
The following unit tests demonstrate the problem: http://pastie.org/1288565 However, it could just be that I am not using Zend_Form correctly. Has anyone run into this problem before? -- *Hector Virgen* Sr. Web Developer http://www.virgentech.com On Wed, Nov 10, 2010 at 1:53 PM, Hector Virgen <djvir...@gmail.com> wrote: > Hello, > > What is the most straight-forward way to mark an element as invalid and > display a custom error message *without using validators*? > > For example, we are using a web service to process data submitted via a web > form. I have set up validators in my form for the basic things like required > fields and string length, but sometimes there is additional validation that > occurs on the web-service side that could prevent the process from > continuing. In that event I'd like to take the error message from the web > service and inject it into the offending form element to be displayed to the > user. > > I've looked over the API and > documentation<http://framework.zend.com/manual/en/zend.form.elements.html#zend.form.elements.validators.errors>and > there are two methods that seem likely candidates for doing my bidding: > > *Zend_Form_Element#setErrors(array $errors)* > *Overwrite any previously set error messages and flag as failed validation > * > > *Zend_Form_Element#setErrorMessages(array $errorMessages)* > *Same as addErrorMessages(), but clears custom error message stack first* > > If I use setErrors() and pass in an array with a single error message, the > message is duplicated when rendering. > > If I use setErrorMessages() and pass in an array with a single error > message, the message is not displayed at all when rendering. > > I found that if I call both methods and pass in the same single-value array > to both of them, it works as expected -- I get my nice error message on the > rendered form. > > Is this typically how it should be done? Or am I missing something? Thanks! > > -- > *Hector Virgen* > Sr. Web Developer > http://www.virgentech.com > >