douhan8009 2014-06-30 07:05
浏览 32
已采纳

Symfony2:订单表单字段

Consider this piece of code:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text')
        ->add('description', 'textarea')
        ->add('status', 'choice', array('choices' => array('online' => 'online', 'offline' => 'offline')))
        ->add('save', 'submit');
}

But when I render this form I get it in the order

description
name
status

How can I change this besides writing the form template by hand.

  • 写回答

3条回答 默认 最新

  • dpvp56187 2014-06-30 10:11
    关注

    You are hoping to control the render order from your PHP, but in reality, your PHP ought not know about, nor care about, how information is rendered. The order of rendering is handled in the template, preferably via twig, as it is the template's responsibility to handle this. It is a straightforward process, and gives you plenty of control. The following Twig template ought to fix your order:

    {{form_start(nameOfFormVariablePassedFromController}}
        {{form_label(nameOfFormVariablePassedFromController.name)}}
        {{form_widget(nameOfFormVariablePassedFromController.name)}}
    
        {{form_label(nameOfFormVariablePassedFromController.description)}}
        {{form_widget(nameOfFormVariablePassedFromController.description)}}
    
        {{form_label(nameOfFormVariablePassedFromController.status)}}
        {{form_widget(nameOfFormVariablePassedFromController.status)}}
    
        {{form_widget(nameOfFormVariablePassedFromController.save)}}
    {{form_end(nameOfFormVariablePassedFromController}}
    

    It is easy to add classes to these labels and widgets for styling, by adding extra arguments. I can help with this if you need further clarification.

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

报告相同问题?

悬赏问题

  • ¥15 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao