从控制器增加 symfony2 形式的标签索引

问题描述 投票:0回答:1

我正在寻找一种获取标签索引的方法,以将其显示在 sonata_type_collection 中的每个字段中: 实际上我在管理类的 configureFormFields 函数中使用了这段代码:

 ->add('TrainingGoals', 'sonata_type_collection', array(
                    'by_reference' => false,
                    'btn_add' => false,
                    'required' => false,
                    'label' => true,
                    'type_options' => array('delete' => false),
                    'cascade_validation' => true,
                        ), array(
                    'edit' => 'inline',
                    'inline' => 'table',
                ))

我使用此函数仅渲染目标表单的五个实例:

public function getNewInstance() {
        $object = parent::getNewInstance();

        for ($i = 0; $i < 5; $i++) {

            $trainingGoals = new TrainingGoals();
            $trainingGoals->setGoal('');
            $trainingGoals->setTraining();
            $object->addTrainingGoal($trainingGoals);
        }
        return $object;
    }

但是我只得到所有渲染字段的一个标签,这有什么意义吗?我怎样才能渲染这样的标签:

 Goals 1:
 Goals 2:
 Goals 3: 
 ...
php symfony symfony-forms sonata-admin
1个回答
1
投票

最后我得到了解决方案: 首先,我在 MyBundle/Form/Type 下创建了一个新类型,希望扩展我创建的此类的 sonata_type_collection:

    namespace AAA\AABundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class CustomSonataTypeCollectionType extends AbstractType
{

    public function getParent()
    {
        return 'sonata_type_collection';
    }
    public function getName() {
        return 'custom_field';
    }

}

然后我将其声明为服务:

  mybundle.TrainingBundle.form.type.CustomSonataTypeCollection:
        class: AAA\TrainingBundle\Form\Type\CustomSonataTypeCollectionType
        tags:
            - { name: form.type, alias: custom_field }

我将此方法添加到我的管理类后:

 public function getFormTheme() {
        return array_merge(
                parent::getFormTheme(), array('AAATrainingBundle:Form:custom_field_edit.html.twig')
        );

最后我在视图下创建了一个 Form 文件夹并将我的自定义表单放在那里:

{% block sonata_admin_orm_one_to_many_widget %}
    {% if sonata_admin.name == 'TrainingGoals' %}

        {% set associationAdmin = sonata_admin.field_description.associationadmin %}

        <div>
            {% for nested_group_field in form.children %}
                <ul class="nav nav-tabs">
                    {% for name, form_group in associationAdmin.formgroups %}
                        {{ associationAdmin.trans('goal', {}, 'AAATrainingBundle') }}  {{ loop.parent.loop.index }}

                    {% endfor %}
                </ul>

                <div class="tab-content">
                    {% for name, form_group in associationAdmin.formgroups %}
                        <div class="tab-pane {% if loop.first %}active{% endif %}" id="{{ associationAdmin.uniqid }}_{{ loop.parent.loop.index }}_{{ loop.index }}">
                            <fieldset>
                                <div class="sonata-ba-collapsed-fields">
                                    {% for field_name in form_group.fields %}
                                        {% set nested_field = nested_group_field.children[field_name] %}
                                        {% if associationAdmin.formfielddescriptions[field_name] is defined %}
                                            {{ form_row(nested_field, {
                                                            'inline': 'natural',
                                                            'edit'  : 'inline'
                                                        }) }}
                                            {% set dummy = nested_group_field.setrendered %}
                                        {% else %}
                                            {{ form_row(nested_field) }}
                                        {% endif %}
                                    {% endfor %}
                                </div>
                            </fieldset>
                        </div>
                    {% endfor %}
                </div>

                {% if nested_group_field['_delete'] is defined %}
                    {{ form_row(nested_group_field['_delete']) }}
                {% endif %}
            {% endfor %}
        </div>



    {%  else %}
        {% include 'SonataDoctrineORMAdminBundle:CRUD:edit_orm_one_to_many.html.twig' %}
    {% endif %}
{% endblock %}
        }
© www.soinside.com 2019 - 2024. All rights reserved.