Steven Szymczak wrote:
> 
> I have a login form with just two fields for username and password. 
> There are length and regex validators attached to both, but what I would 
> like to do is return a single generic "login failed" message if any of 
> the validators fail.  How might I go about accomplishing that?
> 

I don't know if this is best practice or not, but in my projects I attach
"generic" form error messages using: $form->addError("Invalid Login
Credentials.") and then manually iterate over those messages in the view:

Action Controller:

$form = new My_Form_Login();

if ($this->getRequest()->isPost())
{
        if ($form->isValid($_POST))
        {
                $email = $form->getValue('email');
                $password = $form->getValue('password');

                // ...do the authentication...
                $result = $auth->authenticate($authAdapter);
                if ($result->isValid()) {
                        // success!
                        $auth->getStorage()->write($data);
                        $this->_redirect('/');
                } else {
                        // failure: clear database row from session
                        $form->addError("Invalid Login Credentials.");
                }
        }
}

$this->view->login_form = $form;

View:

<?php echo $this->genericFormErrors($this->login_form); ?>
<?php echo $this->login_form; ?>

Helper:

class My_View_Helper_GenericFormErrors extends Zend_View_Helper_Abstract
{
        public function genericFormErrors($form)
        {
                $xhtml = '';

                if ($errors = $form->getErrorMessages())
                {
                        $xhtml .= '<ul class="main-form-errors">';
                        foreach ($errors as $error)
                                $xhtml .= '<li>' . $error . '</li>';
                        $xhtml .= '</ul>';
                }

                return $xhtml;
        }
}



Steven Szymczak wrote:
> 
> Also, I've noticed that if my regex validator fails a blank page is 
> returned.  No failure message, no form, nothing.  It makes wonder if 
> regex validators don't have default error messages attached to them?
> 

A blank form isn't normal/expected. Potentially your error_reporting is
filtering the errors, or the form is not being set for the view in a certain
case. Without seeing code it is hard to say. 
This may help in regards to setting error messages for the regex validators:
http://www.nabble.com/Form-Validator:-multiple-RegEx-patterns-on-a-single-element-td17479561.html

Cheers!
-- 
View this message in context: 
http://www.nabble.com/Zend_Form%3A-single-error-message-for-logins-tp20252828p20253811.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to