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 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程