且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将未绑定字段添加到 Symfony 中的表单,否则该字段绑定到实体?

更新时间:2023-10-05 23:11:16

在您的表单中添加一个带有 false property_path 的文本字段:

In your form add a text field with a false property_path:

$builder->add('extra', 'text', array('property_path' => false));

然后您可以访问控制器中的数据:

You can then access the data in your controller:

$extra = $form->get('extra')->getData();

更新

自 Symfony 2.1 以来的新方法是使用 mapped 选项并将其设置为 false.

The new way since Symfony 2.1 is to use the mapped option and set that to false.

->add('extra', null, array('mapped' => false))

感谢 Henrik Bjørnskov 的更新信息(下方评论)

Credits for the update info to Henrik Bjørnskov ( comment below )