I've got my form set up, as well as a user controller to handle logins. When a login fails, I want to display a generic "login failed" message, regardless of which validator failed; however it just keeps giving me the default error messages. I've tried various solutions, including setting a class property $_error = TRUE to determine if the form should be invalidated when redisplayed by the index action, but to no avail.

The second problem is that if my regex validator fails, the form isn't [re]rendered, leaving the page essentially blank.

Here is the relevant code:

        public function indexAction() {
                $login_form = $this->buildForm();
                
                $this->view->assign('login_form', $login_form);
        }
        
        public function loginAction() {
                if (!$this->getRequest()->isPost()) {
                        return $this->_forward('index');
                }
                
                $login_form = $this->buildForm();
                if (!$login_form->isValid($_POST)) {
                        $this->view->assign('login_form', $login_form);
                        
                        return;
                }
                
                $this->auth($login_form->getValues());
        }
        
        public function buildForm() {
                $login_form = new Zend_Form();
                $login_form->setAction('/user/login')
                                   ->setMethod('post');
                                
                $form_class = $login_form->getDecorator('HtmlTag');
                $form_class->setOption('class', 'login');
                        
                $username = $login_form->createElement('text', 'username');
                $username->setLabel('Username:');
                $username->addValidator('regex', FALSE, 
array('/[^!#%\*=\/\\\,]/'))
                                 ->addValidator('stringLength', FALSE, array(2, 
20))
                                 ->setRequired(TRUE);
                                
                $password = $login_form->createElement('password', 'password');
                $password->setLabel('Password:');
                $password->addValidator('stringLength', FALSE, array(6))
                                 ->setRequired(TRUE);
                
                $submit = $login_form->createElement('submit', 'Login');
                $submit->setDecorators(array(
                        'ViewHelper',
                        new Zend_Form_Decorator_HtmlTag(array('tag' => 
'div'))));
                                
                $login_form->addElement($username)
                                   ->addElement($password)
                                   ->addElement($submit);
                                        
                return $login_form;
        }

Reply via email to