如何在同一个动作中验证3个单独的形式?

时间:2012-12-06 15:00:27

标签: php javascript jquery html zend-framework

因此,我在同一页面上有3个表单和相同的控制器操作,当我点击其中一个提交按钮时,它会验证所有表单而不是我点击的表单。

如何将其与验证分开?

这里是我的代码:

public function signUpAction()
{
    $firstName = $this->getRequest()->getParam('firstName');
    $lastName = $this->getRequest()->getParam('lastName');
    $email = $this->getRequest()->getParam('email');
    $emailAdrress = $this->getRequest()->getParam('Email_Address');
    $password = $this->getRequest()->getParam('password');

    $signupForm = new Application_Form_UserSignUp();
    $loginForm = new Application_Form_UserLogin();
    $retreivePasswordForm = new Application_Form_UserRetreivePassword();

    if ($this->getRequest()->isPost('signupForm'))
    {
        /*********** Sign Up Form ***********/
        if ($signupForm->isValid($this->getRequest()->getParams()))
        {
            $user = $this->_helper->model('Users')->createRow($signupForm->getValues());   
            if ($user->save())
            {
                Zend_Session::rememberMe(186400 * 14);
                Zend_Auth::getInstance()->getStorage()->write($user);
                $user->sendSignUpEmail();
                $this->getHelper('redirector')->gotoRoute(array(), 'invite');
                return;
            }
            else 
            {
            }
        }
        else
        {
            // something
        }
    }
    if ($this->getRequest()->isPost('loginForm'))
    {
        /************ Login Form ************/
        if ($loginForm->isValid($this->getRequest()->getParams()))
        {
            $user = $this->_helper->model('Users')->createRow($loginForm->getValues()); 
            $user = $this->_helper->model('Users')->fetchRowByFields(array('email' => $email, 'hash' => $password));

            if($user) 
            {
                Zend_Session::rememberMe(86400 * 14);
                Zend_Auth::getInstance()->getStorage()->write($user);
                $this->getHelper('redirector')->gotoRoute(array(), 'invite');
                return;
            } 
            else {
                // Error message
                $this->view->errorMsg = "<b>password</b> - invalid, please try again! *";
            }               
        }
        else
        {
            // something
        }
    }
    if ($this->getRequest()->isPost('retreivePasswordForm'))
    {
        /****** Retreive Password Form ******/
        if ($retreivePasswordForm->isValid($this->getRequest()->getParams()))
        {
            $user = $this->_helper->model('Users')->createRow($retreivePasswordForm->getValues()); 
            $user = $this->_helper->model('Users')->fetchRowByFields(array('email' => $emailAdrress));
            if($user)
            {
                Zend_Auth::getInstance()->getStorage()->write($user);
                $user->sendRetreiveEmail();

                $_SESSION['email'] = $emailAdrress;

                $redirector = $this->_helper->getHelper('redirector');
                $redirector->setCode(301)->setUseAbsoluteUri();
                $newPath = 'http://refer.lavasoft.com/#retreive_sent';
                $redirector->gotoUrl($newPath);

                //$this->view->assign('sendEmail', $emailAddress);
            }
            else
            {
            }
        }
        else
        {
            // something
        }

    }

    $this->view->retreivePasswordForm = $retreivePasswordForm;
    $this->view->loginForm = $loginForm;
    $this->view->signupForm = $signupForm;
}

1 个答案:

答案 0 :(得分:1)

这不是ZF的工作原理。除非在代码中覆盖isPost(),否则不能将表单名称传递给isPost()。

您可以做的是定义您的所有字段,如:

loginForm[name]
loginForm[password]

signupForm[name]

等 然后只需检查isset($_POST['loginFom'])

这应该可以正常工作。

或者使用名为formName的隐藏字段并检查代码的值是什么。

您还可以为每个表单使用不同的操作,最后重定向到signUpAction,这样可以毫不费力地输出相同的结果。