Voici mon problème j'ai un champ de type SELECT , entityType de symfony 4, et j'aimerais que lorsque l'utilisateur vienne faire une modification d'un élément déjà en bdd, celui-ci mets un class='SELECTED' sur l'élément récupéré en bdd, car je récupère toutes les valeurs des champs textfield sans problème mais pas pour les champs SELECT, Help .
Mon formulaire :
Code php : 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 <?php namespace App\Form; use App\Entity\Cit; use App\Entity\Client; use App\Entity\ParametrageAssistantes; use App\Entity\ParametrageCommerciaux; use App\Entity\ParametrageSections; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\DateTimeType; use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class CitType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('numero', TextType::class, [ 'label' => 'NUMERO CIT ' ]) ->add('dateouverture', DateType::class, [ 'attr' => ['class' => 'js-datepicker'], 'html5' => false, 'widget' => 'single_text', ]) ->add('modifdate', DateType::class, [ 'attr' => ['class' => 'js-datepicker'], 'html5' => false, 'widget' => 'single_text', 'required' => false, ]) // ->add('modifid') // ->add('careal') ->add('client', EntityType::class, [ 'class' => Client::class, 'mapped' => true, 'query_builder' => function(EntityRepository $cli){ return $cli->createQueryBuilder('c') ->orderBy('c.designationclient', 'ASC'); }, 'choice_attr' => function($choice, $key, $value) { if($choice == $value){ return ['class' => 'selected']; }else { return ['class' => 'selectedTEST']; } }, //'choice_label' => 'DESIGNATIONCLIENT', 'label' => 'Client', ]) ->add('codeassistante', EntityType::class, [ 'class' => ParametrageAssistantes::class, 'query_builder' => function(EntityRepository $codeass){ return $codeass->createQueryBuilder('c') ->orderBy('c.nomAssistante', 'ASC'); }, 'choice_label' => function(ParametrageAssistantes $assistantes) { return sprintf('(%s) %s', $assistantes->getCodeass(), $assistantes->getNomAssistante()); }, /* 'choice_value' => function (ParametrageAssistantes $assistantes) { return $assistantes ? $assistantes->getCodeass() : ''; },*/ //'choice_label' => 'NOM_ASSISTANTE', 'label' => 'Assistante', ]) ->add('codecommercial', EntityType::class, [ 'class' => ParametrageCommerciaux::class, 'query_builder' => function(EntityRepository $codecom){ return $codecom->createQueryBuilder('cc') ->orderBy('cc.nomCommercial', 'ASC'); }, 'choice_label' => function(ParametrageCommerciaux $commerciaux) { return sprintf('(%s) %s', $commerciaux->getCodecom(), $commerciaux->getNomCommercial()); }, //'choice_label' => 'NOM_COMMERCIAL', 'label' => 'Commercial' ]) ->add('codesection', EntityType::class, [ 'class' => ParametrageSections::class, 'query_builder' => function(EntityRepository $codesec){ return $codesec->createQueryBuilder('cs') ->orderBy('cs.codesecteur', 'ASC'); }, 'choice_label' => function(ParametrageSections $sections) { return sprintf('%s', $sections->getCodesecteur()); }, // 'choice_label' => 'CODESECTEUR', 'label' => 'Code Secteur', ]) ->add('datesolde', DateType::class, [ 'html5' => false, 'widget' => 'single_text', 'attr' => ['class' => 'js-datepicker'], 'required' => false, ]) /* ->add('delai0', DateType::class, [ 'html5' => false, 'widget' => 'single_text', 'attr' => ['class' => 'form-control input-inline js-datepicker'], 'html5' => false, ])*/ // ->add('margereal') ->add('navireservice') ->add('numappeloffre') ->add('objet') ->add('nbOi') ->add('refus', TextareaType::class, array( "label" => "Motif Refus", 'attr' => ['class' => 'form-control'], 'required' => false, )) ->add('prixRefus', CheckboxType::class, array( "required" => false, )) ->add('autres', CheckboxType::class, array( "required" => false, )) ->add('delaiRefus', CheckboxType::class, array( "required" => false, )) ->add('creationCommandeClient', SubmitType::class, ['label' => 'Créer commande']) ->add('creationFactureClient', SubmitType::class, ['label' => 'Créer Facture']) ->add('creationOrdreIntervention', SubmitType::class, ['label' => 'Créer Ordre Intervention']) ->add('creationOrdreMission', SubmitType::class, ['label' => 'Créer Ordre Mission']) ; } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => Cit::class, ]); } }
Mon edit.html.twig
Code twig : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 {% extends 'base.html.twig' %} {% block title %}Edit Cit{% endblock %} {% block body %} {{ parent() }} <h1>Edit Cit</h1> {{ include('cit/_form.html.twig', {'button_label': 'Mettre à jour'}) }} <a href="{{ path('cit_index') }}"><button>Revenir en arrière</button></a> {{ include('cit/_delete_form.html.twig') }} {% endblock %}
Mon controller :
Code php : 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 <?php namespace App\Controller; use App\Entity\Cit; use App\Entity\Client; use App\Form\CitRecherchePersonnaliseType; use App\Form\CitType; use App\Repository\CitRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; /** * @Route("/cit") */ class CitController extends AbstractController { /** * @Route("/", name="cit_index", methods={"GET","POST"}) */ public function index(Request $request): Response { $cit = new Cit(); $cits = null; $formBuilder = $this->get('form.factory')->createBuilder(CitRecherchePersonnaliseType::class, $cit); $form = $formBuilder->getForm(); // Si la requête est en POST if ($request->isMethod('POST')) { $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $cit = $form->getData(); if($cit->getClient() == "" && $cit->getObjet() == ""){ $cits = $this->getDoctrine()->getRepository(Cit::class) ->findByNumCit($cit->getNumero()); }else{ $cits = $this->getDoctrine()->getRepository(Cit::class) ->findByNumClientDesign($cit->getNumero(), $cit->getClient(), $cit->getObjet()); } return $this->render('cit/index.html.twig', [ 'form' => $form->createView(), 'cits' => $cits, ]); } } return $this->render('cit/index.html.twig', [ 'form' => $form->createView(), 'cits' => $cits, ]); } /** * @Route("/new", name="cit_new", methods={"GET","POST"}) */ public function new(Request $request): Response { $cit = new Cit(); $form = $this->createForm(CitType::class, $cit); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($cit); $entityManager->flush(); if ($form->getClickedButton() && 'creationCommandeClient' === $form->getClickedButton()->getName()) { return $this->redirectToRoute('commandeclient_new', array('NUMERO' => $cit->getNumero())); } if ($form->getClickedButton() && 'creationFactureClient' === $form->getClickedButton()->getName()) { return $this->redirectToRoute('facture_new', array('NUMERO' => $cit->getNumero())); } if ($form->getClickedButton() && 'php bin/console' === $form->getClickedButton()->getName()) { return $this->redirectToRoute('di_new', array('NUMERO' => $cit->getNumero())); } if ($form->getClickedButton() && 'creationOrdreMission' === $form->getClickedButton()->getName()){ return $this->redirectToRoute('ordre_mission_new', array('NUMERO' => $cit->getNumero())); } return $this->redirectToRoute('cit_index'); } return $this->render('cit/new.html.twig', [ 'cit' => $cit, 'form' => $form->createView(), ]); } /** * @Route("/{numero}", name="cit_show", methods={"GET"}) * @param Cit $cit * @return Response */ public function show(Cit $cit): Response { return $this->render('cit/show.html.twig', [ 'cit' => $cit, ]); } /** * @Route("/{numero}/edit", name="cit_edit", methods={"GET","POST"}) */ public function edit(Request $request, Cit $cit): Response { $form = $this->createForm(CitType::class, $cit); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->getDoctrine()->getManager()->flush(); if ($form->getClickedButton() && 'creationCommandeClient' === $form->getClickedButton()->getName()) { return $this->redirectToRoute('commandeclient_new', array('NUMERO' => $cit->getNumero())); } if ($form->getClickedButton() && 'creationFactureClient' === $form->getClickedButton()->getName()) { return $this->redirectToRoute('facture_new', array('NUMERO' => $cit->getNumero())); } if ($form->getClickedButton() && 'creationOrdreIntervention' === $form->getClickedButton()->getName()) { return $this->redirectToRoute('di_new', array('NUMERO' => $cit->getNumero())); } if ($form->getClickedButton() && 'creationOrdreMission' === $form->getClickedButton()->getName()){ return $this->redirectToRoute('ordre_mission_new', array('NUMERO' => $cit->getNumero())); } return $this->redirectToRoute('cit_index'); } return $this->render('cit/edit.html.twig', [ 'cit' => $cit, 'codeass' => $cit->getCodeassistante(), 'form' => $form->createView(), ]); } /** * @Route("/{numero}", name="cit_delete", methods={"DELETE"}) */ public function delete(Request $request, Cit $cit): Response { if ($this->isCsrfTokenValid('delete'.$cit->getNumero(), $request->request->get('_token'))) { $entityManager = $this->getDoctrine()->getManager(); $entityManager->remove($cit); $entityManager->flush(); } return $this->redirectToRoute('cit_index'); } }
Partager