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

Is there a way to show a semi-filled form after posting?

lets say I have a form. I submit it, the validator fails, and I redirect myself to it. I dont want to fill the form again, I want to have the already set values, fields. Is there a way to do it? Its "FlashSession"



85.5k

the way I do it is:

indexAction(){
    $this->view->my_form = new \whatever\MyForms\Form();
}

render form bla bla...


updateAction(){
     $model = new \MyModels\Bla bla bla\Model($_POST);
        if ($model->save() == false){
            $errors = [];
            foreach ($model->getMessages() as $message) {
                $errors[] = $message->getMessage();
            }

            $this->flashSession->message('error', implode('<br>', $errors));
            return $this->dispatcher->forward([ "action" => "index" ]);
        }
}

i hope you can undestand it, cuz my logic there is quite huge, so I deleted a lot of stuff. But basiccaly

$this->dispatcher->forward([ "action" => "index" ]);

should do the magic



28.2k

but if I have to redirect?

the way I do it is:

indexAction(){
  $this->view->my_form = new \whatever\MyForms\Form();
}

render form bla bla...


updateAction(){
   $model = new \MyModels\Bla bla bla\Model($_POST);
       if ($model->save() == false){
           $errors = [];
           foreach ($model->getMessages() as $message) {
               $errors[] = $message->getMessage();
           }

           $this->flashSession->message('error', implode('<br>', $errors));
           return $this->dispatcher->forward([ "action" => "index" ]);
       }
}

i hope you can undestand it, cuz my logic there is quite huge, so I deleted a lot of stuff. But basiccaly

$this->dispatcher->forward([ "action" => "index" ]);

should do the magic



85.5k

i see there is a bind method, it seems like you have to use it

https://docs.phalcon.io/en/latest/reference/forms.html#forms-entities