douge3830 2018-02-16 13:03
浏览 163
已采纳

Symfony form_widget呈现form_row

I try to render a form input in a view but instead of separating label, errors and widget, it renders the whole row of the input.

MyType.php

/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('email', EmailType::class, [
            'label' => 'Email',
            'required' => true,
        ])
        ->add('emailIsPublic', CheckboxType::class, [
            'label' => 'Make e-mail public (can be seen by everyone)',
            'required' => false,
            'attr' => [
                'class' => 'switch',
            ]
        ])
        ->add('submit', SubmitType::class, [
            'label' => 'Save changes',
            'attr' => [
                'class' => 'btn btn-outline-primary float-right',
            ]
        ]);
}

my-view.html.twig

{{ form_start(edit_form) }}
    <div>
        {{ form_label(edit_form.emailIsPublic) }}
    </div>
    <div>
        {{ form_errors(edit_form.emailIsPublic) }}
    </div>
    <div>
        {{ form_widget(edit_form.emailIsPublic) }}
    </div>
{{ form_end(edit_form, {'render_rest': false}) }}

Generated HTML

<form name="appbundle_my" method="post">
    <div></div>
    <div></div>
    <div>
        <div class="form-check">
            <label class="form-check-label">
                <input id="appbundle_my_emailIsPublic" name="appbundle_my[emailIsPublic]" class="switch form-check-input" value="1" type="checkbox"> Make e-mail public (can be seen by everyone)
            </label>
        </div>
    </div>
</form>

As we can see in the generated HTML, form_label and form_errors are empty and form_widget renders what form_row should render. Why?

  • 写回答

2条回答 默认 最新

  • dongsaoshuo4326 2018-03-22 12:35
    关注

    As I saw here: https://github.com/symfony/symfony/issues/26540#issuecomment-375232354

    This behaviour seems to be a bug specific to the usage of the Bootstrap 4 Form theme.

    That's specific to the Bootstrap 4 theme and it seems to be related to the changes made in #26591 [TwigBridge] Make sure we always render errors. Eventhough labels are disabled.

    I will wait for an update of Symfony to solve this.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分