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.

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

报告相同问题?

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?