内容简介:http://stackoverflow.com/questions/12765368/how-do-i-add-an-unbound-field-to-a-form-in-symfony-which-is-otherwise-bound-to-a
也许我错过了明显的,但是如何(或者我可以)添加一个额外的“未绑定”字段到另外绑定到一个实体的Symfony表单?
假设我有一个具有字段first_name和last_name的实体.我在我的表单类buildForm方法中做典型的事情.
$builder
->add('first_name')
->add('last_name')
;
这在我的控制器:
$editForm = $this->createForm(new MyType(), $entity);
这很好,但我想添加另一个文本框,让我们称之为“额外”,并在POST操作中接收值.如果我做$builder-> add(‘extra’),它会抱怨
NoSuchPropertyException in PropertyAccessor.php line 479: Neither the property “extra” nor one of the methods “getExtra()”, “extra()”, “isExtra()”, “hasExtra()”, “__get()” exist and have public access in class…
哪个是对的.我只想使用它从用户处收集一些额外的信息,并且使用它除了与实体一起存储之外.
我知道如何使一个完全独立的形式,但不是一个“混合”.
这可能吗?
在您的表单中添加一个带有false property_path的文本字段:
$builder->add('extra', 'text', array('property_path' => false));
然后,您可以访问控制器中的数据:
$extra = $form->get('extra')->getData();
UPDATE
自Symfony 2.1以来的新方法是使用映射选项并将其设置为false.
– > add(‘extra’,null,array(‘mapped’=> false))
更新信息给HenrikBjørnskov的信用(评论如下)
http://stackoverflow.com/questions/12765368/how-do-i-add-an-unbound-field-to-a-form-in-symfony-which-is-otherwise-bound-to-a
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
A Project Guide to UX Design
Russ Unger、Carolyn Chandler / New Riders Press / 2009-3-23 / USD 39.99
"If you are a young designer entering or contemplating entering the UX field this is a canonical book. If you are an organization that really needs to start grokking UX this book is also for you. " -......一起来看看 《A Project Guide to UX Design》 这本书的介绍吧!