-- dowker <[EMAIL PROTECTED]> wrote
(on Thursday, 03 April 2008, 07:39 AM -0700):
> When trying to validate an email address, many different validation messages
> can appear depending on what the user enters. For example, if a user enters
> "[EMAIL PROTECTED]" (without the quotes), we get the following validation 
> messages:
> 
>     * 'test.' is not a valid hostname for email address '[EMAIL PROTECTED]'
>     * 'test.' appears to be a DNS hostname but cannot extract TLD part
>     * 'test.' does not appear to be a valid local network name
>     * 'test.' appears to be a local network name but local network names are
> not allowed
> 
> While this amount of detail can be very useful in some cases, it really
> isn't when trying to display user-friendly messages in a form.
> 
> Is there a way to limit the messages to something simple like "The email
> address you entered is incorrect."?

Right now, no, not without a little work. Several people have requested
such a feature now, and it is in the tracker.

For the time being, I'd suggest the following:

  * Add an error message to your element as an attribute:
    $element->errorMessage = "custom error message";

  * Create a custom 'Errors' decorator that pulls that attribute and
    displays it:

    class My_Decorator_Errors extends Zend_Form_Decorator_Abstract
    {
        public function render($content)
        {
            $element = $this->getElement();
            if (!isset($element->errorMessage)) {
                // Fallback to original errors decorator if property not
                // present
                require_once 'Zend/Form/Decorator/Errors.php';
                $decorator = new Zend_Form_Decorator_Errors();
                $decorator->setElement($element)
                          ->setOptions($this->getOptions());
                return $decorator->render($content);
            }

            $view      = $element->getView();
            $html = '<div class="error">' 
                  . $view->escape($element->errorMessage) 
                  . '</div>';

            $placement = $this->getPlacement();
            $separator = $this->getSeparator();
            switch ($placement) {
                case self::PREPEND:
                    return $html . $separator . $content;
                case self::APPEND:
                default:
                    return $content . $separator . $html;
            }
        }
    }

  * Make sure that you have a decorator path to the above set for your
    elements:

    $form->addElementPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');

and that should do it.

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to