简体   繁体   English

Symfony 2表单提交集表单下拉值

[英]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. 我如何在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. $editForm->submit()方法是保留它的方法。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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