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

Tutorial 2: companies edit form

Hi,

I have a question regarding the company edit form in INVO tutorial. I still can not come up with the answer to how it is possible that when I click save button on edit page I am forwarded to company/index and form on this index site is populated with the values of the company that was edited before even though the index action is calling getForm function which uses $form = new Form($entity) and thus should create new empty form object for the index view ($this->view->form = $this->getForm();) which is definitely happening when I am entering this site clicking the Company tab. I realized that it is possible to reset the form using

$form = $this->getForm(); $form->clear();

but that actully does not explain the issue because calling $form = $this->getForm(); should be enough to get clear form.

I guess that this is just a lack of my php knowledge by any help would be appreciated.



98.9k

$form = $this->getForm(); just creates a new instance of the form, values that fill the form with default values are taken from $_POST, this is why you have to call $form->clear(); not sure if this answers your question.



718

Thanks for the answer, but this is probably not what I am looking for. When you click save button on edit form you go to save Action (public function saveAction()) where you get the values from $_POST:

First to load the selected company to the model

    $id = $this->request->getPost("id", "int");
    $companies = Companies::findFirstById($id);

and then to change the values

    $companies->id = $this->request->getPost("id", "int");
    $companies->name = $this->request->getPost("name", "striptags");
    $companies->telephone = $this->request->getPost("telephone", "striptags");
    $companies->address = $this->request->getPost("address", "striptags");
    $companies->city = $this->request->getPost("city", "striptags");

and save them

    if (!$companies->save()) {

But then you are forwarded to companies/index/

    return $this->forward("companies/index/");

Which is calling index action (public function indexAction()) and there you call the getForm() with no parameters

    $this->view->form = $this->getForm();

and from that you get the form with filled information but there was no call like

    $this->view->form = $this->getForm($companies);

and that is what I don't understand. Where do the data come from? Only standard blank companie/index form should be loaded. There is only

    $this->forward("companies/index/");

no redirect so the data are probably still there somewhere (probably in $_POST thats right) but I don't see where are they loaded into the form in the index action or index view.