We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Dispatcher triggers isPost after forward

Hi fellow Phalconers,

I'm using the dispatcher to forward a user to the next form/view after first saving and validating the current form (see controller snippet below):

if ($form1->save())) {
    return $this->dispatcher->forward(array(
        'controller' => 'index',
        'view' => 'form2'
    ));
}

However, this is triggering the isPost() on form2, and therefore the form's validation.

if ($this->request->isPost()) {
    // form processing, checking valid, save, etc.
}

Is there anyway to stop the forward from triggering isPost? I did try:

if (($this->request->isPost()) && (!$this->dispatcher->wasForwarded())) {
    // form processing, checking valid, save, etc.
}

But unfortunately this then stops 'form2' from being processed.



16.3k

What about redirecting using response?

I am trying to keep the URL the same - e.g. mydomain.com/apply

Hmm, Phalcon\Http\Request doesn't have a wasForwarded method, are you trying to use Phalcon\Dispatcher::wasForwarded like this:

if (($this->request->isPost()) && (!$this->dispatcher->wasForwarded()))

?

edited Jul '14

Yes Max you are correct, sorry was a typo (have edited the original post).

However as mentioned running that code prevents the validation from occuring when I do then press to submit.



993
Accepted
answer
edited Jul '14

form1Action:

$this->view->form = $form;
$this->dispatcher->setActionName('form1');

form2Action:

if ((($this->request->isPost())) && ($this->dispatcher->getPreviousActionName() != 'form1')) {
}

$this->view->form = $form;
$this->dispatcher->setActionName('form2');