dsfds2343
2016-12-08 10:45
浏览 60
已采纳

Symfony2:CollectionType表单

I create an add form for evenement entity with document(entity) so i use this link https://symfony.com/doc/current/form/form_collections.html and this is the result: enter image description here my problem is how to remove id of document from the form?

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

</div>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题