【问题标题】:Symfony3 - How to render Embedded collection of FormsSymfony3 - 如何呈现嵌入的表单集合
【发布时间】:2017-12-30 14:24:26
【问题描述】:

我还没有找到手动呈现包含集合的表单的解决方案。 这是我在树枝中的代码:

<ul id="document-fields-list" data-prototype="{{ form_widget(formulario.documentos.vars.prototype)|e }}">
    <div><button class="pull-right" href="#" id="add-another-document">Agregar documento</button></div>
    {% for documento in formulario.documentos %}
        <li>
            {{ form_label(documento) }}
            {{ form_widget(documento) }}
            <a href="#" class="remove-item">Eliminar</a>
        </li>
    {% endfor %}
</ul>

【问题讨论】:

    标签: symfony collections twig display symfony3.x


    【解决方案1】:

    表单类型

    在您的情况下,我们需要为 PersonaDocumento 创建 formType。想象一下,这个实体有字段 documentName:

    class PersonaDocumentoType extends AbstractType
    {
        /**
         * @param FormBuilderInterface  $builder
         * @param array                 $options
         * @SuppressWarnings(unused)
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('documentName', TextType::class, [
                    'label'                 => false,
                    'translation_domain'    => 'messages'
                ])
            ;
        }
    
        /**
         * @return string
         */
        public function getName()
        {
            return 'app_persona_documento_type';
        }
    
        /**
         * @return null|string
         */
        public function getBlockPrefix()
        {
            return 'app_persona_documento';
        }
    
        /**
         * @param OptionsResolver $resolver
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class'            => PersonaDocumento::class,
                'csrf_protection'       => true,
                'validation'            => true,
            ));
        }
    }
    

    包含集合的表单

    考虑你的实体Formulario。它与 PersonaDocumento 具有 OneToMany 关系。表格将是:

    class FormularioFormType extends AbstractType
    {
        /**
         * @param FormBuilderInterface  $builder
         * @param array                 $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            // ...
    
            $builder
                ->add('documentos', CollectionType::class, [
                    'entry_type'    => PersonaDocumentoType::class,
                    'entry_options' => [
                        'label'         => false,
                    ],
                    'label'         => false,
                    'allow_add'     => true,
                    'allow_delete'  => true,
                    'by_reference'  => false,  // Very important thing!
                ])
            ;
        }
    
        // ...
    }
    

    收集小部件

    我们有一个表单 (FormularioFormType),其中包含类型为 PersonaDocumentoType 的小表单集合。

    您可以使用标准小部件在文件中创建新小部件,以及 file is fields.html.twig 的名称:path_to_your_project/src/AppBundle/Resources/views/Form/fields.html.twig

    区块名称为app_persona_documento_widget

    因此,fields.html.twig 的示例:

    {% trans_default_domain 'messages' %}
    
    {% block app_persona_documento_widget %}
        {% spaceless %}
            <div class="form-group" {{ block('widget_container_attributes') }}>
                <div class="col-sm-12">
                    {{ form_widget(form.name, {'attr' : { 'placeholder' : 'app.form.label.name'|trans , 'class' : 'form-control' }}) }}
                </div>
            </div>
        {% endspaceless %}
    {% endblock app_persona_documento_widget %}
    

    还要注意“app_persona_documento_widget”——由你的PersonaDocumentoType 的getBlockPrefix() 加上字符串“_widget”组装而成

    在 config.yml 中注册新的表单主题

    # Twig Configuration
    twig:
        debug: '%kernel.debug%'
        strict_variables: '%kernel.debug%'
        form_themes:
            # other form themes
            # ...
            - 'AppBundle:Form:fields.html.twig'
    

    以父表单呈现集合

                {{ form_start(formulario_form) }}
                    <div class="form-group">
                        <label for="" class="col-sm-2 control-label">
                            Label
                        </label>
                        <div class="col-sm-10 documentos" data-prototype="{{ form_widget(formulario_form.documentos.vars.prototype)|e('html_attr') }}">
                            {% for document in formulario_form.documentos %}
                                <div>
                                    {{ form_widget(document) }}
                                </div>
                            {% endfor %}
                        </div>
                    </div>
                    <span>
                        {{ form_errors(formulario_form) }}
                    </span>
                    {#  Here you can add another fields of form   #}
                {{ form_end(formulario_form) }}
    

    当然,您还需要按钮:每个“Documento”项目都有一个“添加另一个文档”按钮和“删除”按钮。

    Symfony 文档建议我们为此目的使用 JavaScript。 你可以阅读更多here in official docs

    你也可以安装Ninsuo/symfony-collection - 一个管理从 Symfony 集合中添加、删除和移动元素的 jQuery 插件

    【讨论】:

    • 我想知道是否还有其他方法,例如:更改 app/config 中的某些装置中的某些内容,因为它是 symfony 的预配置。谢谢
    • 我不想为我的应用程序中的所有集合和表单创建这个
    • @LucasLeonelAzzollini,这是官方的方式。并且没有更方便的方法来添加集合。但是!
    • @LucasLeonelAzzollini 但是!您可以为集合安装捆绑包 - Ninsuo/symfony-collection。你可以在 Packagist 上找到它。此捆绑包为您提供导航按钮(添加和删除)。
    • 我解决了twig中包含form_theme的问题
    猜你喜欢
    • 2017-11-08
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2017-02-01
    • 2017-07-24
    • 1970-01-01
    相关资源
    最近更新 更多