-- bfoust <[EMAIL PROTECTED]> wrote
(on Wednesday, 21 May 2008, 07:53 PM -0700):
>
> A validator's setMessage("...") may be used to change the error message when
> a validator fails. For example:
>
> $formElement->getValidator('StringLength')->setMessage("Your password
> must be at least 6 characters long.");
>
>
> However, the NotEmpty validator (created automatically when
> setRequired(true) has been called on an element) cannot have it's message
> changed in the same way. This is because it is added, not at the time of the
> setRequired(true) call, but only when validate() is called:
>
>
> First off, wouldn't it make more sense if the default error message was
> "This value may not be left blank." in the first place, rather than, "Value
> is empty, but a non-empty value is required"?
>
> But secondly, there should be a consistent method of changing any error
> message. The current implementation is inconsistent, because it prevents the
> specified API for changing a validator's message from functioning. And that
> is because the validator does not exist, until validate() is called on the
> element:
The easy way to correct this is to explicitly set a NotEmpty validator
at the top of your validation chain. The auto-insertion of the NotEmpty
validator is basically to enforce the 'required' restriction placed on
the element; you can always add this validator yourself. (After reading
the rest of the email, you appear to have discovered this. (: )
BTW, the canonical and suggested way to customize error messages is to
use translations (which you ask about below). Why? Because they provide
a single place for you to manage all error strings, provide the simplest
way to affect all forms in your application, provide consistent messages
for all forms and input filters in your application, and because it
encourages developers to internationalize their applications.
<snip>
> One possible fix would be for the ZF team to create the NotEmpty
> validator within setRequired. That way, later calls to
> getValidator('NotEmpty') will succeed.
This is a reasonable solution. Please place an issue in the tracker for
this! :-)
> I suppose another solution might be to create a Translator. Does a
> translator get called even for translations to English?
Yes.
> When writing new code, the simplest fix while we await an update to ZF
> 1.5.1, is to still use setRequired(true), but explicitly create the NotEmpty
> validator beforehand:
Right. :-)
--
Matthew Weier O'Phinney
Software Architect | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/