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

Displaying error messages after a redirect

Note: Not sure why my code isn't formatted properly, it looks fine in the textarea prior to saving. Sorry!

I've been learning Phalcon by reviewing the Vokuro App. For my test application I'm following a similar approach to Forms and Validation by using Form Classes, however, I prefer to redirect after submit, so I need to rely on a session variable to retain the Form validation messages. I want to display inline error messages underneath each form field.

public function signupAction()
{
    $form = new \Testapp\Forms\SignUpForm();

    if ( $this->request->isPost() )
    {
        if ( $form->isValid($this->request->getPost()) != false )
        {
             // Do stuff on success
        }
        else
        {
                $this->session->set('messages', $form->getMessages());
                $this->response->redirect('sign-up');
        }
    }
}

The messages method in my SignUpForm class, as it is on the Vokuro App. Obviously, in my case it isn't going to work because there are no messages, because of the redirect. What's the best approach to get this method to use the data from my session variable? Do I have to re-write it completely? I was hoping there was an easier way e.g: some how setting the messages object that the hasMessagesFor and getMessagesFor rely on.

public function messages($name)
{
    if ($this->hasMessagesFor($name)) {
        foreach ($this->getMessagesFor($name) as $message) {
            $this->flash->error($message);
        }
    }
}


98.9k

You have to use the session flasher instead of the direct flash: https://docs.phalcon.io/en/latest/reference/flash.html#adapters