Zend表单中间步骤,如何修改正常代码

jbl*_*lue 2 php zend-framework zend-form

如何使用zend获得多步骤类型表单.这就是我想要做的......

通常我有一个表单,用户填写表单.当表单提交(发布)并且有效时,我会执行最终操作(添加到数据库或其他任何内容).

public function indexAction(){
   $form = new Application_Form_Test();

   if(form is valid){
      //do the final thing.. add to the database or whatever
   }
   $this->view->form = $form;
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是插入一个中间步骤.当用户提交表单时,我不想立即插入数据库.我想首先计算一些额外的数据,除了刚刚输入的输入外,还向用户显示计算数据,并显示确认按钮.如果用户按下提交,我会做最后添加到数据库的事情,否则我会取消整个操作.那么有人可以想到一个简单的方法来做到这一点,而不需要做很多疯狂的黑客攻击吗?

Mar*_*cin 7

我准备了一份解决问题的可行方法草案.基本上它涉及两个动作(indexAction和index2Action)和一个会话.

的indexAction

public function indexAction() {
    $form = new Application_Form_Test();

    if ($this->getRequest()->isPost()) {
        if ($form->isValid($_POST)) {

            // calculate some additional data
            $calculatedData = someCalulations();

            // if form is valid than save submitted and calculated data in a session
            $addAccInfoNamespace = new Zend_Session_Namespace('data');
            $addAccInfoNamespace->postData = $form->getValues();
            $addAccInfoNamespace->calculatedData = $calculatedData;
            $addAccInfoNamespace->lock();

            // and redirect to the second stage (or middle stage)
            return $this->_redirect('index/index2');
        }
    }
    $this->view->form = $form;
}
Run Code Online (Sandbox Code Playgroud)

index2Action

 public function index2Action() {

        // retrieve data saved in the first stage (indexAction).
        $sessionData = new Zend_Session_Namespace('data');

        if (null === $sessionData->postData || null === $sessionData->calculatedData) {
            // if no data go to index, or throw execption or whatever.
            return $this->_redirect('index/index');
        }

        // get the data
        $oldPostData = $addAccInfoNamespace->postData;
        $calculatedData = $sessionData->calculatedData;

        // form that only shows cunfirmation button
        // However, if you want to show data in this form, that you need to 
        // populate Application_Form_Test2 with whatever data you want.
        $form = new Application_Form_Test2();


        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {

                if (!$form->confirm->isChecked()) {
                    // if confirm submit button was NOT clicked go to e.g. index
                    // This may mean that ,e.g. cancel submit button was click.
                    return $this->_redirect('index/index');
                }


                //do the final thing.. add to the database or whatever


                // don't need this session namespace anymore
                Zend_Session::namespaceUnset('data');

                // and redirect to e.g. success confirmation page
                return $this->_redirect('index/success');
            }
        }

        // show confirmation button and additional calculated data
        $this->view->calculatedData = $calculatedData;
        $this->view->form = $form;
    }
Run Code Online (Sandbox Code Playgroud)

你怎么看?

  • 哇,这就像我们在那里分享脑空间几分钟:) (3认同)