如何单独显示 sfWidgetFormChoice 的单选按钮?
我想以这种方式呈现 symfony 表单:
我的问题涉及单选按钮小部件:我不不知道如何单独呈现每个选择,以便在这些单选按钮之间显示其他字段。
有没有办法用 symfony 形式来实现这一点?
谢谢!
I'd like to render a symfony form this way:
My problem concerns the radio buttons widget: I don't know how to render each choice individually, in order to display other fields between those radio buttons.
Is there a way to achieve this with symfony forms?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Symfony 让这件事变得有点困难。您想要一个可从视图调用的
renderChoice( $value)
方法,但这需要更改或扩展 sfFormField。更改它不是面向未来的,扩展它还需要您更改一些 symfony 变量,以便您视图中可访问的字段是这个新类的实例。我不知道这个选项是否存在。
相反,我选择将 sfWidgetFormSelectRadio 扩展为以下非常简单的类,该类扩展了
formatChoices
。它将检查您是否只要求一种选项。如果是,它会渲染该标签并返回它,否则它会通过parent::
正常渲染。这可以让您从视图中调用:
$form['field']->render(array('only_choice'=> $value))
其中
$value
是要呈现的单选选项的索引:代码很尴尬,但在实践中很容易使用,并且将来不会中断。
Symfony makes this a little difficult to do. You'd like a
renderChoice( $value)
method that's callable from the view, but that would require changing or extending sfFormField.Changing it isn't future proof, and extending it would also require you to change some symfony variable so the fields accessible in your view are instances of this new class. I don't know if that option exists.
Instead, I opted to extend sfWidgetFormSelectRadio into the following very simple class which extends
formatChoices
. It will check if you're asking for only one option. If you are, it renders that tag and returns it, otherwise it renders normally viaparent::
.This lets you call from your view:
$form['field']->render(array('only_choice'=> $value))
where
$value
is the index of the radio option you want to render:Awkward code, but easy enough to use in practice and it won't break in the future.
标准小部件不允许您在一个字段的元素之间插入另一个字段;请参阅 sfWidgetFormSelectRadio::formatChoices() 来了解原因。我的第一反应是扩展 sfWidgetFormSelectRadio,包括一个 renderChoice( $value ) 方法。
The standard widgets are not going to allow you to inject another field in between elements of one field; see
sfWidgetFormSelectRadio::formatChoices()
for why this is. My first reaction is to extend sfWidgetFormSelectRadio, including arenderChoice( $value )
method.如何定义具有相同名称的单独
sfWidgetFormSelectRadio
小部件和不同的价值观?How about defining separate
sfWidgetFormSelectRadio
widgets with the same name and different values?