duandang2838 2017-06-08 02:16
浏览 43
已采纳

如何格式化/控制Symfony ChoiceType的输出

Is it possible to iterate over ChoiceType values in Symfony 3? I can setup the values fine, but they just output in big block for which i can't control. I would like to loop over each value and format it/put in table/div/columns etc.

$builder->add('tOptions', ChoiceType::class, array(
    'choices'  => array(
        'one' => true,
        'two' => true,
        'three' => true,
        'four' => true,
        'five' => true,
    ),
    'expanded' => true,
    'multiple' => true,
    'required' => false,
));

Note: I am not outputting in twig, just in PHP.

echo $view['form']->widget($form['tOptions']);

Results in:

<div id="t_options_tOptions">
<input type="checkbox" id="t_options_tOptions_0" name="t_options[tOptions][]" value="0">
<label for="t_options_tOptions_0">one</label>

<input type="checkbox" id="t_options_tOptions_1" name="t_options[tOptions][]" value="1">
<label for="t_options_tOptions_1">two</label>

<input type="checkbox" id="t_options_tOptions_2" name="t_options[tOptions][]" value="2">
<label for="t_options_tOptions_2">three</label>

<input type="checkbox" id="t_options_tOptions_3" name="t_options[tOptions][]" value="3">
<label for="t_options_tOptions_3">four</label>

<input type="checkbox" id="t_options_tOptions_4" name="t_options[tOptions][]" value="4">
<label for="t_options_tOptions_4">five</label>
</div>

How does one iterate over these input options, in order to wrap each in div or split into two even columns etc.

  • 写回答

2条回答 默认 最新

  • duanbiao4025 2017-06-08 04:30
    关注

    To customize that you'd have to create a custom template for the choices widget. Specifically, you could extend the choice_widget_options.html.php file.

    To apply the styling you can follow the Form Theming in PHP documentation. It's worth noting that extending the template with Twig would be simpler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页