Mopa bootstrap example nested collection parent

Simple Parent Collection


        $builder
            ->add(
                'myCollection',
                'collection',
                [
                    'type' => new ExampleNestedCollectionChildType(),
                    'allow_add' => true,
                    'label' => 'parent collection'
                ]

            );
                 

And its Childform


        $builder
            ->add(
                'myCollectionChild',
                'collection',
                [
                    'type' => 'text',
                    'allow_add' => 'true',
                    'label' => 'smth'
                ]
            );
             

FormType Code

// ExampleNestedCollectionParentType.php

<?php

namespace Mopa\Bundle\BootstrapSandboxBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Mopa\Bundle\BootstrapSandboxBundle\Form\Type\ExampleNestedCollectionParentType
 *
 * @author Ivan Molchanov <ivan.molchanov@opensoftdev.ru>
 */
class ExampleNestedCollectionParentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'myCollection',
                'collection',
                array(
                    'type' => new ExampleNestedCollectionChildType(),
                    'allow_add' => true,
                    'label' => 'parent collection',
                    'options' => array(
                        'label' => 'child collection'
                    )
                )

            );
    }

    public function getName()
    {
        return 'mopa_bootstrap_example_nested_collection_parent';
    }

    /**
     * Configures the options for this type.
     *
     * @param OptionsResolver $resolver The resolver for the options
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        // TODO: Implement configureOptions() method.
    }
}

Template Code

{# MopaBootstrapSandboxBundle:Examples:nestedCollections.html.twig #}

{% extends "MopaBootstrapSandboxBundle::layout.html.twig" %}
{% from 'MopaBootstrapBundle::flash.html.twig' import session_flash %}
{% form_theme form _self %}

{% block headline %}Collection Forms{% endblock headline %}

{% block content %}
<div class="col-lg-6">
    <form class="form-horizontal" id="your_form_name" method="post">
        {{ form_widget(form) }}
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Save changes</button>
            <button type="reset" class="btn">Cancel</button>
        </div>
    </form>
</div>
<div class="col-lg-6">
    <div class="row">
        <div class="col-sd-12">
             <h3>Simple Parent Collection</h3>

                  <pre class="prettyprint">

        $builder
            ->add(
                'myCollection',
                'collection',
                [
                    'type' => new ExampleNestedCollectionChildType(),
                    'allow_add' => true,
                    'label' => 'parent collection'
                ]

            );
                 </pre>
        </div>
        <div class="col-sd-12">
             <h4>And its Childform</h4>

              <pre class="prettyprint">

        $builder
            ->add(
                'myCollectionChild',
                'collection',
                [
                    'type' => 'text',
                    'allow_add' => 'true',
                    'label' => 'smth'
                ]
            );
             </pre>
        </div>
    </div>
</div>
{% endblock content %}

{% set showTemplate = _self %}{% set showForm = formType %}
Fork me on GitHub