Symfony - 集合表单原型不采用 html 类属性

我正在尝试添加带有幻灯片集合的轮播,但无法设置嵌入表单的样式。我的原型跳过了表单中的一些类属性。 文档在这里

我的轮播类型:

    public function buildForm(FormBuilderInterface $builder, array $options)

    {

        $builder->add('slides', CollectionType::class, [

            'entry_type' => SlideType::class,

            'allow_add' => true,

            'allow_delete' => true,

            'prototype' => true,

            'by_reference' => false

        ]);

    }

我的幻灯片类型:


    public function buildForm(FormBuilderInterface $builder, array $options)

    {

        $builder

            ->add('title', TextType::class, [

                'attr' => [

                    'class' => 'form-control'

                ],

                'required' => false

            ])

            ->add('url', UrlType::class, [

                'attr' => [

                    'class' => 'form-control'

                ],

                'required' => false

            ])

            ->add('file', FileType::class);

    }

多么新的添加表单显示:


<div class="slide">

  <div id="carousel_slides_3">

    <div>

      <label for="carousel_slides_3_title">Title</label>

      <input type="text" id="carousel_slides_3_title" name="carousel[slides][3][title]" class="form-control">

    </div>

    <div>

      <label for="carousel_slides_3_url">Url</label>

      <input type="text" id="carousel_slides_3_url" name="carousel[slides][3][url]" class="form-control" inputmode="url">

    </div>

    <div>

      <label for="carousel_slides_3_file" class="required">File</label>

      <input type="file" id="carousel_slides_3_file" name="carousel[slides][3][file]" required="required">

    </div>

  </div>

</div>

新表单不显示class="form-group"属性。


我的配置或 javascript 有什么问题?


DIEA
浏览 99回答 1
1回答

RISEBY

它的工作原理是在 SlideType 端添加类属性并form_row在模板端使用:我的幻灯片类型:&nbsp; &nbsp; public function buildForm(FormBuilderInterface $builder, array $options)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $builder&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->add('title', TextType::class, [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'row_attr' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'form-group'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'attr' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'form-control'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->add('url', UrlType::class, [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'row_attr' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'form-group'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'attr' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'form-control'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->add('file', FileType::class, [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'row_attr' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'form-group'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'attr' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'form-control'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; }我的 carousel_edit.html.twig:{% block body %}&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card card-default">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-body">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form_start(form) }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="slides" data-prototype="{{ form_widget(form.slides.vars.prototype)|e('html_attr') }}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% for slide in form.slides %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="slide">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form_row(slide.title) }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form_row(slide.url) }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form_row(slide.file) }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-success add_item_link" data-collection-holder-class="slides">+</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-danger remove_item_link" data-collection-holder-class="slides">-</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-footer pt-4 pt-5 mt-4 border-top">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-primary btn-default">Save</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ form_end(form) }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>{% endblock %}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript