使用setAction时,Zend表单验证失败

时间:2014-08-12 11:43:16

标签: php zend-framework

我想验证表单,如果验证通过,则必须将其提交给connectTracking Action。

目前,表单在通过验证之前会被提交给connectTracking操作。

控制器:

public function easyAction(){
    $auth = Zend_Auth::getInstance();
    $user_id = $this->_helper->Utilities->getCurrentUserId();

    if ($auth->hasIdentity()) {
        $form       = new Application_Form_easy();
        $form->submit->setLabel('Submit');
        $this->view->form = $form;  
        $formData = $this->getRequest()->getPost();

        if ( ($this->getRequest()->getPost('easyform', false)) &&  ($form->isValid($this->getRequest()->getPost())) ) { 
            $username = $form->getValue('username');                
            $password = $form->getValue('password');
            $releasedata = array('username' => $username, 'password' => $password);
        }
    }
}

/application/forms/easy.php

class Application_Form_easy extends Zend_Form {
    public function init() {
        $this->setMethod('post');
        $this->setName('easyform');
        $this->setAction('/index.php/releases/connectTracking'); # Submitting to different action

        $username = new Zend_Form_Element_Text('username');
        $username->setLabel('Username * :')
                ->setRequired(true)
                ->addValidator('NotEmpty', true, array('messages' => 'Please enter Username'));

        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password * :')
                ->setRequired(true)
                ->addValidator('NotEmpty', true, array('messages' => 'Please enter password '));

                $submit = new Zend_Form_Element_Submit('submit');
                $submit->setAttrib('id', 'easyform');
                $submit->setAttrib('name', 'easyform');
                $this->addElements(array($username, $password, $submit));
    }
}

请您告诉我如何验证表格然后提交行动

非常感谢!

1 个答案:

答案 0 :(得分:1)

基本上isValid是验证表单的方法:

$form = new Application_Form_easy();
$formData = $this->_request->getPost();
if($form->isValid($formData)){
 //Form is valid, do your stuff
} else {
 //Form is not valid re-populate data
 $form->populate($formData);
}

在您的情况下,您正在设置此/index.php/releases/connectTracking的表单操作,并且您已在easyAction中编写了验证码,这是错误的。 你有两种方法来达到你的要求;

1)根据/controllerName/easy设置表单的操作,并在easyAction(您现在正在执行)中验证您的表单,如果表单有效,则将控件重定向到releases/connectTracking操作。

2)另一种方法是在releases/connectTracking操作中编写验证代码,如果表单无效,则再次将控件重定向到easy操作,并重新填充所有POST数据表格。如果表单有效,请继续releases/connectTracking行动。

E.g。第一个解决方案:

控制器:

public function easyAction(){
    $auth = Zend_Auth::getInstance();
    $user_id = $this->_helper->Utilities->getCurrentUserId();

    if ($auth->hasIdentity()) {
        $form       = new Application_Form_easy();
        $form->submit->setLabel('Submit');
        $this->view->form = $form;  
        $formData = $this->getRequest()->getPost();

        if ($form->isValid($formData)) { 
            $username = $form->getValue('username');                
            $password = $form->getValue('password');
            $releasedata = array('username' => $username, 'password' => $password);
            //Form is valid, hence forward controller to another action with formData
            $this->_request->setPost(array('formData' => $formData));
            //Write actual module name in the below line
            $this->_forward('connectTracking', 'releases', 'moduleName');
            //$this->_redirect('/releases/connectTracking');
        } else {
          $form->populate($formData);
        }
    }
}

形式:

class Application_Form_easy extends Zend_Form {
    public function init() {
        $this->setMethod('post');
        $this->setName('easyform');
        //Write actual name of your controller here
        $this->setAction('/controllerName/easy'); # Submitting to same action

        $username = new Zend_Form_Element_Text('username');
        $username->setLabel('Username * :')
                ->setRequired(true)
                ->addValidator('NotEmpty', true, array('messages' => 'Please enter Username'));

        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password * :')
                ->setRequired(true)
                ->addValidator('NotEmpty', true, array('messages' => 'Please enter password '));

                $submit = new Zend_Form_Element_Submit('submit');
                $submit->setAttrib('id', 'easyform');
                $submit->setAttrib('name', 'easyform');
                $this->addElements(array($username, $password, $submit));
    }
}