如何在Zend Framework 1.12中检查控制器中的表单验证

时间:2014-04-24 12:22:40

标签: php validation zend-framework zend-framework2

如何检查表单是否经过验证,如果表单不符合验证,如何显示错误消息

在zf2中我们写为

userController

$request = $this->getRequest();
$form = new Form_LoginForm();
if($request->isPost()){
    if($form->isvalid($this->_request->getPost())){
        $authAdapter = $this->getAuthAdapter();
        $username = 'john';
        $password = '123';
        $authAdapter->setIdentity($email)
                    ->setCredential($password);
        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($authAdapter);
        if($result->isvalid()){
            $identity = $authAdapter->getResultRowObject();
            $authStorage = $auth->getStorage();
            $authStorage->write($identity);
            $this->_helper->redirector(register/user);
            echo 'valid';
        } else {
            $this->view->errorMessage = "User name or password is incorrect";
        }
    }
}

上面的代码在zend framework2中,我需要Zend framework 1.12中的代码。我如何从login.phtml获取表单到userController以及如何编写代码来检查验证,如第4行所写,这是用zend framework2编写的

我的login.phtml表单是

    <form action="<?php echo $this->url(array('controller'=>'user','action'=>'login'),
    'default',true);?>" method="post">
    Email: <input type="text" name="user_email" type="email"            oninvalid="setCustomValidity('Plz enter valid email ')"      onchange="try{setCustomValidity('')}catch(e){}" required />
    Password: <input type="password" name="password" required />
    <input type="submit" name="submit" value="SUBMIT"  />
    </form>

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样......

    $form           =   new Form_LoginForm();

if ($this->getRequest()->isPost()) 
{
    if ($form->isValid($_POST)) 
    {
        $formData       =   $this->_getFormData();
        $authAdapter    =   $this->_getAuthAdapter($formData);
        $auth           =   Zend_Auth::getInstance();
        $result         =   $auth->authenticate($authAdapter);

        /**
         * Authentication failed
         */
        if (!$result->isValid()) 
        {
            switch ($result->getCode())
            {
                // Username doesn't exist
                case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND   :   $yourLogic;
                                                                        break;
                // Username and Password combination was incorrect
                case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID   :   $yourLogic;
                                                                        break;
                // General error message
                default :   $yourLogic;
            }
        }
        /**
         * Authentication success
         */
        else 
        {
            // Store the identity as an object where the password column has been omitted
            $data   =   $authAdapter->getResultRowObject(null, 'password');
        }
    }
}
相关问题