简体   繁体   中英

Symfony 2 Form Submit Set Form Dropdown Value

I have a page that submits back to itself, it has some form choice fields that I would like to set the value of them based upon what was chosen initially on the submit. How do I do that in Symfony.

For instance, I have a field supplier:

 {{ form_widget(form.supplier) }}

When I choose a supplier and hit submit, I want supplier to be the value that was just entered when the form loads back up on the page.

Thanks!

That should happen with the default controller setup. You need to bind the form data to the form, and then redisplay the form in the same action.

This should be all you need for a form that will maintain it's state. The $editForm->submit() method is what preserves it.

public function editAction()
{
  $defaultData = array('supplier' => new Supplier());
  $editForm = $this->createForm(new SupplierType(), $defaultData);
  $editForm->submit($this->getRequest());

  return array('form' => $editForm->createView());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM