2

I have a problem with form collection on symfony. I have 3 entities Article, AdditionnalFile, AdditionnalInformation

Article entity

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="articles")
 * @ORM\JoinColumn(nullable=false)
 * @Gedmo\Versioned
 */
private $category;

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\AdditionnalInformation", mappedBy="article", cascade={"persist", "remove"})
 * @ORM\JoinColumn(nullable=true)
 */
private $additionnalInformations;

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\AdditionnalFile", mappedBy="article", cascade={"persist", "remove"})
 * @ORM\JoinColumn(nullable=true)
 */
private $additionnalFiles;

AdditionnalInformation entity

    /**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Article", inversedBy="additionnalInformations")
 */
private $article;

/**
 * @ORM\ManyToMany(targetEntity="UserLdapBundle\Entity\Group", inversedBy="additionnalInformations")
 * @ORM\JoinColumn(nullable=false)
 *
 * @Assert\Count(
 *      min = 1,
 *      max = 5,
 *      minMessage = "Il faut au minimum 1 groupe autorisé",
 *      maxMessage = "Il faut au maximum {{ limit }} groupe autorisé"
 * )
 */
private $groups;

/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255)
 * @Gedmo\Versioned
 * @Assert\Type(type="string")
 * @Assert\NotBlank()
 */
private $title;


/**
 * @var string
 *
 * @ORM\Column(name="text", type="text")
 * @Gedmo\Versioned
 * @Assert\Type(type="string")
 * @Assert\NotBlank()
 */
private $text;

I don't give the last entity because it's not important I have create a form type for AdditionnalFile

    /**
 * @param FormBuilderInterface $builder
 * @param array                $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'title',
            TextType::class,
            array(
                'attr'  => array(
                    'placeholder' => 'Titre'
                ),
                'label' => 'Titre :'
            )
        )
        ->add(
            'text',
            TextareaType::class,
            array(
                'attr' => array(
                    'placeholder' => 'Texte'
                ),
                'label'        => 'Texte :'
            )
        )
        ->add(
            'groups',
            EntityType::class,
            array(
                'attr'         => array(
                    'placeholder' => 'Droits'
                ),
                'class'        => 'UserLdapBundle:Group',
                'choice_label' => 'name',
                'expanded'     => true,
                'multiple'     => true,
                'label'        => 'Accessible pour :'
            )
        );
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => AdditionnalInformation::class,
    ));
}

And I have create my article formtype who "embed" my additionnalInformationType

    /**
 * @param FormBuilderInterface $builder
 * @param array                $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'title',
            TextType::class,
            array(
                'attr'  => array(
                    'placeholder' => 'Titre'
                ),
                'label' => 'Titre :'
            )
        )
        ->add(
            'category',
            EntityType::class,
            array(
                'attr'         => array(
                    'placeholder' => 'Catégorie'
                ),
                'class'        => 'AppBundle\Entity\Category',
                'choice_value' => 'id',
                'choice_label' => 'name',
                'multiple'     => false,
                'label'        => 'Catégorie :'
            )
        )
        ->add(
            'text',
            TextareaType::class,
            array(
                'attr'     => array(
                    'placeholder' => 'Texte',
                    'class'       => 'tinymce'
                ),
                'label'    => 'Texte :',
                'required' => false
            )
        )
        ->add(
            'tags',
            TextType::class,
            array(
                'attr'  => array(
                    'placeholder' => 'Tags'
                ),
                'label' => 'Tags :'
            )
        )
        ->add(
            'ticketNumber',
            TextType::class,
            array(
                'attr'     => array(
                    'placeholder' => 'Numéro de ticket, 301, 302,'
                ),
                'label'    => 'Numéro(s) de ticket :',
                'required' => false
            )
        )
        ->add(
            'groups',
            EntityType::class,
            array(
                'attr'         => array(
                    'placeholder' => 'Droits'
                ),
                'class'        => 'UserLdapBundle:Group',
                'choice_label' => 'name',
                'expanded'     => true,
                'multiple'     => true,
                'label'        => 'Accessible pour :'
            )
        )
        ->add(
            'additionnalInformations',
            CollectionType::class,
            array(
                'entry_type' => AdditionnalInformationType::class,
                'allow_add'    => true,
                'label' => 'Information(s) additionnel(s) :',
                'prototype' => true
            )
        )
        ->add(
            'additionnalFiles',
            CollectionType::class,
            array(
                'entry_type' => AdditionnalFileType::class,
                'allow_add'    => true,
                'label' => 'Fichier(s) :',
                'prototype' => true
            )
        )
        ->add(
            'save',
            SubmitType::class,
            array(
                'label' => 'Sauvegarder',
                'attr'  => array(
                    'class' => 'btn-primary'
                )
            )
        );

But now i have some question... :) How can i custom the prototype? i want to use a bootstrap panel and put the additionnalInformation form inside. And duplicate this for add other AdditionnalInformation Is that possible ?

2 Answers 2

1

Here is a working example which I have used:

{%  block _article_additionnalInformations_entry_row %}
<br>
<div class="panel panel-primary">
    <div class="panel-heading">Information supplémentaire 
        <a href="#" class="btn btn-xs btn-danger pull-right">
            <span class="glyphicon glyphicon-remove confirmation-suppression"></span>
        </a>
    </div>
    <div class="panel-body">
        <div class="row">
            <div class="col-lg-8">
                {{ form_row(form.title) }}
                {{ form_row(form.text) }}
            </div>
            <div class="col-lg-4">
                {{ form_row(form.groups) }}
            </div>
        </div>
    </div>
</div>
{% endblock %}
Sign up to request clarification or add additional context in comments.

Comments

0

You should try to write a custom twig theme. You can find more information on this page.

You can for example try to put this code in your template (where you render your form):

{% form_theme form _self %}

{% block _additionnalFiles_entry_widget %}
    <tr>
        <td>{{ form_widget(form.task) }}</td>
        <td>{{ form_widget(form.dueDate) }}</td>
    </tr>
{% endblock %}

Just make sure to use the right block name. You can do that by understanding how form fragment are named or just inspect a {{ dump(form) }} in your template.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.