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"



64.2k

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



12.2k

but if I have to redirect?

the way I do it is:

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

render form bla bla...

```php

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



64.2k

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

https://docs.phalconphp.com/en/latest/reference/forms.html#forms-entities