dquh37673 2016-03-21 18:47
浏览 52
已采纳

Symfony / TWIG:无法为具有唯一ID的表单设置自定义主题(createNamedBuilder)

I wanted to replace Symfony's default radio button widget with Bootstrap's Radio Button Group. I was trying to achieve that by setting a custom theme for an individual field in Symfony's form created with createNamedBuilder function. I failed, because custom field themes require the ID of the field which is dynamic in my case..

I know that having variables in theme names is impossible in TWIG, but maybe there is an alternative approach that I could use to resolve my issue.

EXPLANATION:

I have a Controller which creates multiple instances of one form type in a loop. The name of the form is dynamically created by concatenating requestform_ and the ID of the form:

public function listAction(Request $request)
{
    $entityManager = $this->getDoctrine()->getManager();
    $requestForms = $entityManager->getRepository('AppBundle:RequestForm')->findBy(array(), array('id' => 'ASC'));
    $forms = array();   
    foreach ($requestForms as $requestForm) {
        $formBuilder = $this->get('form.factory')->createNamedBuilder('requestform_'.$requestForm->getId(), RequestFormType::class, $requestForm);
        $form = $formBuilder->getForm()->handleRequest($request);
        $forms[] = $form->createView();
    }
    return $this->render('form/index.html.twig', array('forms' => $forms));
}

buildForm function of the RequestFormType prepares radio button widget and looks like this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('status', EntityType::class, array(
        'class' => 'AppBundle:Status',
        'choice_label' => 'name',
        'expanded' => true,
        'multiple' => false
    ));
}

The Controller renders the following template:

{% block content %}
    {% for form in forms %}
        {{ form_start(form) }}
            {{ form_widget(form.status) }}
        {{ form_end(form) }}
    {% endfor %}
{% endblock %}

Which is themed to show Bootstrap's button group selector instead of radio buttons:

{% block _requestform_1_status_widget %}
    <div id={{ form.vars.id }} class="btn-group" data-toggle="buttons">
        {% for status in form.children %}
            <label for={{ status.vars.id }} class="btn btn-primary required custom-button-radio">
                <input type="radio" id={{ status.vars.id }} name={{ status.vars.full_name }} required="required" value={{ status.vars.value }}>
                {{ status.vars.label }}
            </label>
        {% endfor %}
    </div>
{% endblock _requestform_1_status_widget %}

As you may have noticed this will work only for requestForm ID 1 and will not work for other ID values due to TWIG not allowing variables in block names, i.e. {% block _requestform_VARIABLE_status_widget %}.

Any help much appreciated! Thanks

  • 写回答

1条回答 默认 最新

  • douwang6635 2016-03-21 20:49
    关注

    My gut feeling is that you might want to have only one form that includes a CollectionType field with entry_type = RequestFormType::class, and then in the twig, apply a form to the "outer" form as a whole. You should still be able to manage things the way you wanted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料