Bonjour,
J'ai un soucis avec un formulaire imbriqué.
Voici le code du formulaire en question :
Voici le code du formulaire imbriqué :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 <?php namespace KG\BeekeepingManagementBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Doctrine\ORM\EntityRepository; class TranshumanceType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $propertyPathToEmplacement = 'emplacement'; $transhumance = $builder->getData(); $colonie = $transhumance->getColonie(); $exploitation = $colonie->getRucher()->getExploitation()->getId(); $transhumance->setDate(new \DateTime()); $transhumances = $colonie->getTranshumances(); $startDate = $colonie->getDateColonie(); if($transhumances->last()){ if($transhumances->last()->getId() == $builder->getData()->getId()){ $len = count($transhumances) - 2; if($transhumances{$len}){ $startDate = date_add($$transhumances{$len}->getDate(),date_interval_create_from_date_string("1 days")); } } else{ $startDate = date_add($transhumances->last()->getDate(),date_interval_create_from_date_string("1 days")); } } $startDateFormat = date_format($startDate,"Y-m-d"); $builder ->add('date', 'collot_datetime', array( 'pickerOptions' => array('format' => 'dd/mm/yyyy', 'autoclose' => true, 'startDate' => (string)$startDateFormat, 'endDate' => date("Y-m-d"), 'startView' => 'month', 'minView' => 'month', 'maxView' => 'month', 'todayBtn' => false, 'todayHighlight' => true, 'keyboardNavigation' => true, 'language' => 'fr', 'forceParse' => true, 'pickerReferer ' => 'default', 'pickerPosition' => 'bottom-right', 'viewSelect' => 'month', 'initialDate' => date("Y-m-d"), ), 'read_only' => true )) ->add('rucherto', 'entity', array( 'class' => 'KGBeekeepingManagementBundle:Rucher', 'choice_label' => 'nom', 'empty_value' => '', 'attr' => array( 'class' => 'rucher_selector', ), 'query_builder' => function (EntityRepository $repository) use ($exploitation) { $qb = $repository->queryfindByExploitationId($exploitation); return $qb; } )) ->add('colonie', new TranshumanceColonieType(), array( 'label' => false, )); } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'KG\BeekeepingManagementBundle\Entity\Transhumance' )); } /** * @return string */ public function getName() { return 'kg_beekeepingmanagementbundle_transhumance'; } }
Et celui du formulaire imbriqué dans l'imbriqué
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 <?php namespace KG\BeekeepingManagementBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class TranshumanceColonieType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('ruche', new TranshumanceRucheType(), array( 'label' => false, )); } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'KG\BeekeepingManagementBundle\Entity\Colonie' )); } /** * @return string */ public function getName() { return 'kg_beekeepingmanagementbundle_colonie'; } }:
Voici le code du listenner :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 <?php namespace KG\BeekeepingManagementBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use KG\BeekeepingManagementBundle\Form\EventListener\TranshumerEmplacementFieldSubscriber; class TranshumanceRucheType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $propertyPathToEmplacement = 'emplacement'; $builder ->addEventSubscriber(new TranshumerEmplacementFieldSubscriber($propertyPathToEmplacement)); } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'KG\BeekeepingManagementBundle\Entity\Ruche' )); } /** * @return string */ public function getName() { return 'kg_beekeepingmanagementbundle_ruche'; } }
Voici le code dans mon contrôleur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 <?php namespace KG\BeekeepingManagementBundle\Form\EventListener; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\PropertyAccess\PropertyAccess; use Doctrine\ORM\EntityRepository; class TranshumerEmplacementFieldSubscriber implements EventSubscriberInterface { private $propertyPathToEmplacement; public function __construct($propertyPathToEmplacement) { $this->propertyPathToEmplacement = $propertyPathToEmplacement; } public static function getSubscribedEvents() { return array( FormEvents::PRE_SET_DATA => 'preSetData', FormEvents::PRE_SUBMIT => 'preSubmit' ); } private function addEmplacementForm($form, $rucher = null) { $formOptions = array( 'class' => 'KGBeekeepingManagementBundle:Emplacement', 'choice_label' => 'nom', 'empty_value' => '', 'mapped' => false, 'attr' => array( 'class' => 'emplacement_selector', ), 'query_builder' => function (EntityRepository $repository) use ($rucher) { $qb = $repository->queryfindByRucherId($rucher); return $qb; } ); $form->add($this->propertyPathToEmplacement, 'entity', $formOptions); } public function preSetData(FormEvent $event) { $form = $event->getForm(); $this->addEmplacementForm($form); } public function preSubmit(FormEvent $event) { $form = $event->getForm(); $rucher = $form->getParent()->getParent()->get('rucherto')->getData(); $this->addEmplacementForm($form, $rucher); } }
Je ne comprends pas pourquoi au moment du persist, $transhumance->getColonie()->getRuche()->getEmplacement() n'a pas changé alors que pourtant dans le formulaire le champ emplacement est bien mappé...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 $transhumance = new Transhumance($colonie); $form = $this->createForm(new TranshumanceType, $transhumance); if ($form->handleRequest($request)->isValid()){ //$transhumance->getColonie()->setRucher($transhumance->getRucherto()); $em = $this->getDoctrine()->getManager(); $em->persist($transhumance); $em->flush(); $request->getSession()->getFlashBag()->add('success','Transhumance créée avec succès'); return $this->redirect($this->generateUrl('kg_beekeeping_management_view_ruche', array('ruche_id' => $transhumance->getColonie()->getRuche()->getId()))); }
Est ce que quelqu'un aurait une idée?
Merci à tous.
Partager