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.

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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向