phpsymfonysymfony-formssonata-admin

Increase label index in symfony2 form from the controller


I'm looking for a way to get label index to show it with every field from sonata_type_collection: Actually I used this code in the configureFormFields function of my admin class:

 ->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',
                ))

And I use this function to render only five instance of the goals form:

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

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

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

But I get only one label for all rendred fields, does this make any sense ? How can I render label like this :

 Goals 1:
 Goals 2:
 Goals 3: 
 ...

Solution

  • Finaly I got the solution: First I created a new type under MyBundle/Form/Type wish extend the sonata_type_collection I created this class:

        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';
        }
    
    }
    

    Then I declared it as a service :

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

    After i added this method to my Admin class :

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

    Finaly I created a Form folder under the View and puted my custom Form there:

    {% 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 %}
            }