Symfony2:CollectionType表单

时间:2016-12-08 10:45:35

标签: php symfony

我使用文档(实体)为evenement实体创建添加表单,因此我使用此链接https://symfony.com/doc/current/form/form_collections.html,结果如下:enter image description here 我的问题是如何从表单中删除文档的id?

EvenementType

<?php

namespace IcebergBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class EvenementType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
           ->add('type', 'choice', array('label' => 'choisr le type',
    
    'choices'  => array('evenement' => 'Evenement', 'formation' => 'Formation'),
    
    'choices_as_values' => false,
))

            ->add('titre','text', array('label' => 'Titre:', 'attr' => array('class' => 'form-control')))
            ->add('description','text', array('label' => 'Descrption:', 'attr' => array('class' => 'form-control')))
            ->add('datedebut', 'datetime', array('date_widget' => "single_text", 'time_widget' => "single_text",'label' => 'Date debut evenement:'))
            ->add('datefin', 'datetime', array('date_widget' => "single_text", 'time_widget' => "single_text",'label' => 'Date fin evenement:'))
           


               ->add('doc', CollectionType::class, array(

                'label' => 'Document:  ',
                'entry_type' => DocumentType::class,
                'allow_add' => true,
                'allow_delete' => true,

            
        ))

                      ->add('file','file',array('label' => 'Grande Image :  ' ))
                     ->add('file2','file',array('label' => 'Petite Image :  ' ))
          
        ->getForm();

        ;
    }
    
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'IcebergBundle\Entity\Evenement'
        ));
    }
}

EvenementController

   public function newAction(Request $request)
    {


        $evenement = new Evenement();
          $doc= new Document();
 
        $evenement->addDoc($doc);
        $form = $this->createForm('IcebergBundle\Form\EvenementType', $evenement);
        $form->handleRequest($request);

        if ($form->isValid()) {
            

            $em = $this->getDoctrine()->getManager();
                    $doc->upload();
              $evenement->upload();
              $evenement->upload2();


            $em->persist($evenement);
            $em->flush();
             $this->get('session')->getFlashBag()->set(
    'success',
    array(
        'title' => 'Modification avec sucssés !',
        'message' => 'Evenement ajouté .','type'=>'success'
    )
);

            return $this->redirectToRoute('evenement_show', array('id' => $evenement->getId()));
        }

        return $this->render('evenement/new.html.twig', array(
            'evenement' => $evenement,
            'form' => $form->createView(),
        ));
    }

new.html.twig

{% extends 'base_back.html.twig' %}
{% block body %}
 {% for type, flashMessage in app.session.flashbag.all() %}
    <div class="alert alert-{{ flashMessage.type }} fade in">
        <button class="close" data-dismiss="alert" type="button">×</button>
        {% if flashMessage.title is defined %}
        <strong>{{ flashMessage.title }}</strong>
        {{ flashMessage.message }}
        {% else %}
        {{ type }}
        {% endif %}
    </div>
{% endfor %}
    <h1>Ajouter Evenement/Formation</h1>

    {{ form_start(form) }}
        {{ form_widget(form) }}
  <input class="btn btn-primary" type="submit" value="Ajouter" />
    <a class="btn btn-default" href="{{ path('evenement_index') }}">Retourner à la liste<i class="glyphicon glyphicon-eye-open"></i></a>    {{ form_end(form) }}

         
{% endblock %}
{% block javascripts %}

<script src="https://code.jquery.com/jquery.js"></script>
   

{% endblock %}

DocumentType

<?php

namespace IcebergBundle\Form;

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

class DocumentType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
           
            ->add('titre','text', array('label' => 'Titre:', 'attr' => array('class' => 'form-control')))
            ->add('file')
            ->getForm();
        ;
    }
    
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'IcebergBundle\Entity\Document'
        ));
    }
}

2 个答案:

答案 0 :(得分:0)

好的尝试:

'label' => 'Document:  ',
'entry_type' => DocumentType::class,
'allow_add' => true,
'allow_delete' => true,
'entry_options'  => array( 'label' => false )

希望这会对你有所帮助

答案 1 :(得分:0)

如果您在Evenement表单中添加此行Set,则会显示文档的ID。

现在,如果您想要显示文档的->add('doc', CollectionType::class, array(...属性而不是表单中的titre,请在id实体中添加__tostring()函数

Document