I have some problems to make a dynamic form in my Symfony2 project. I try to follow the doc here, but it hard to understand how to really proceed with it. I try to have a form like this on the page.
Indeed, I need to create a dynamic form where dropdown list dynamically change their values when a value is selected from the dropdown list before.
This dynamic form involves 4 entities:
Parcsimmobilier.php
<= ManyToOneEnsembles.php
<= ManyToOneBatiments.php
<= ManyToOneZonestechnique.php
1- the first dopdown concerns the parc, when I choose a value in this dropdown, the ensembles dropdown appears. The values of the ensembles dropdown must belong to the parcs I choose first.
2- the second dropdown with ensembles value: when I selected a value, the third dropdown appears, it concerns the categories of my zones (entity Zonestechnique).
3- the zones categories dropdown have multiple choice, but when I choose the value "outside", the last dropdown with the batiments values appears but with the value "none" inevitably. But if I choose other values than "outside", the batiments dropdown appears with all batiments (belong to the ensemble I choose before).
4- when I make all this steps, the rest of the form appears (form for Zones technique entity)
I have to proceed this form when I want to add/create a new Zones technique in my database.
Here the code for my entities
:
//Parcsimmobilier entity
class Parcsimmobilier
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=150, nullable=false)
*/
private $nom;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Ensembles", mappedBy="parcsimmobilier")
*/
private $ensembles;
//end of Parcsimmobilier entity
//Ensembles entity
class Ensembles
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=150, nullable=false)
*/
private $nom;
/**
* @var \Parcsimmobilier
*
* @ORM\ManyToOne(targetEntity="Parcsimmobilier")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="parcsimmobilier_id", referencedColumnName="id")
* })
*/
private $parcsimmobilier;
//end of Ensembles entity
//Batiments entity
class Batiments
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=150, nullable=true)
*/
private $nom;
/**
* @var \Ensembles
*
* @ORM\ManyToOne(targetEntity="Ensembles")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="ensembles_id", referencedColumnName="id")
* })
*/
private $ensembles;
//end of Batiments entity
//Zonestechnique entity
class Zonestechnique
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=150, nullable=false)
*/
private $nom;
/**
* @var \Batiments
*
* @ORM\ManyToOne(targetEntity="Batiments")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="batiments_id", referencedColumnName="id")
* })
*/
private $batiments;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Categorieszonestechnique")
* @ORM\JoinTable(name="zonestechnique_categorieszonestechnique",
* joinColumns={
* @ORM\JoinColumn(name="zonestechnique_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="categorieszonestechnique_id", referencedColumnName="id")
* }
* )
*/
private $categorieszonestechnique;
Following the doc, this is my formtype, ZonesTechniqueType.php
:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class ZonestechniqueType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('parcsimmobilier', 'entity', array(
'class' => 'EnexgirDatabaseBundle:Parcsimmobilier',
'property' => 'nom',
'multiple' => false,
'required' => true,
'mapped' => false,
'empty_value' => '-- sélectionner un parc --',
'label' => 'Choisir le parc immobilier : ',
'attr' => array('class' => 'col-xs-3 form-control')))
->add('nom')
->add('localisation')
->add('commentaire')
->add('categorieszonestechnique')
->add('batiments');
$formModifier = function (FormInterface $form, Parcsimmobilier $parc = null) {
$ensembles = null === $parc ? array() : $parc->getEnsembles();
$form->add('ensembles', 'entity', array(
'class' => 'EnexgirDatabaseBundle:Ensembles',
'property' => 'nom',
'multiple' => false,
'required' => true,
'mapped' => true,
'empty_value' => '-- sélectionner un ensemble --',
'label' => 'Choisir l\'ensemble : ',
'query_builder' => function(EntityRepository $er) use ($parc) {
return $er->createQueryBuilder('e')
->where("e.parcsimmobilier = :parcsimmobilier_id")
->orderBy('e.nom', 'ASC')
->setParameter(':parcsimmobilier_id', $parc);
},
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be your entity
$data = $event->getData();
$formModifier($event->getForm(), $data->getEnsembles());
}
);
$builder->get('parcsimmobilier')->addEventListener(
FormEvents::POST_SUBMIT,
This is the controller method I use for:
public function addZonesConformitesAction(Request $request) {
$em=$this->getDoctrine()->getManager();
$zoneConformite = $em->getRepository('EnexgirDatabaseBundle:Zonestechnique');
$zoneConformite = new Zonestechnique;
$form=$this->createForm(new ZonestechniqueType(), $zoneConformite);
$form->handleRequest($request);
//instruction if à plusieurs conditions: ici si la method est POST et si le formulaire est valide
if ($request->isMethod('POST') | ($form->isValid())) {
$form->bind($request);
$zoneConformite = $form->getData();
$em->persist($zoneConformite);
$em->flush();
return $this->redirect($this->generateUrl('indexRechercheZones'));
}
//retourner le formulaire d'ajout si c'est invalide
else {
return $this->render('EnexgirGestionEquipementsTechniquesBundle:ZonesTechnique:ajouterZonesTechniqueConformites.html.twig', array('form' => $form->createView() ));
}
}
And finally, the block content of my twig:
{% block content %}
<div class="panel-heading">
<h3 class="panel-title"> Gestion Des Zones techniques -<small> (ajout)</small></h3>
</div>
{# Panel Body #}
<div class="panel-body">
{# Menu #}
<div class="panel-body">
<ul class="nav nav-tabs">
<li role="presentation">
<a href="{{ path('indexRechercheZones') }}">Visualisation des Zones techniques</a>
</li>
<li role="presentation" class="active">
<a>Ajouter une Zone technique et ses Conformités</a>
</li>
</ul>
</div>
{# Formulaire #}
<div class="page-header">
<h4><b><u>Ajouter une zone technique et ses conformites</u></b><small> (créer une zone technique qui n'existe pas encore, ainsi que ses conformités respectives)</small></h4>
<br>
<form action="{{ path('ajouterZonesConformites_process') }}" method="POST" {{ form_enctype(form) }}>
<div>
{{ form_start(form) }}
{{ form_row(form.parcsimmobilier) }} {# <select id="meetup_sport" ... #}
{{ form_row(form.ensembles) }} {# <select id="meetup_position" ... #}
{{ form_end(form) }}
{# validation #}
<input type="submit" value="Ajouter" class="btn btn-success"/>
</div>
<br>
</form>
</div>
</div>
{% endblock %}
For now, I have not introduce the Ajax code in my twig because I have this error:
Attempted to call method "getEnsembles" on class "Enexgir\DatabaseBundle\Entity\Zonestechnique" in C:\wamp\www\MyPathToMyProject\src\Enexgir\DatabaseBundle\Form\ZonestechniqueType.php line 65.
This is the code for the line 65:
$formModifier($event->getForm(), $data->getEnsembles());
I understand that Ensembles.php
and Parcsimmobilier.php
have no relation with Zonestechnique.php
, but I follow the doc, and I don't really understand how this is worked. Someone could help me to understand and make this form?
Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire