Bonjour,
A l'aide d'un formulaire, je crée des points, et je souhaiterais pourvoir les modifier. Donc j'aimerais utiliser le même formulaire et le préremplir avec un point déjà existant.
J'ai cherché sur le forum, et il y a déjà des réponses qui pourraient marcher, mais le problème, c'est que je crée un bouton modifier pour chacun des points et je ne sais pas comment faire pour que ça soit le point du bouton qui remplisse mon formulaire.
Voici mon code :
-voir :
- modifier
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 {# src/Site/PointBundle/Resources/views/defaults/index.html.twig #} {% extends "SitePointBundle::layout.html.twig" %} {% block title %} Accueil - {{ parent() }} {% endblock %} {% block tableau %} <TABLE BORDER="2"> <tr> <th>{{ 'CodeSd' }}</th> <th>{{ 'Id' }}</th> <th>{{ 'Template' }}</th> <th>{{ 'Serveur' }}</th> <th>{{ 'Chemin' }}</th> <th>{{ 'Mini' }}</th> <th>{{ 'Maxi' }}</th> <th>{{ 'Increment' }}</th> <th>{{ 'titre' }}</th> <th>{{ 'Legende' }}</th> </tr> {% for Point in ListePoint %} <tr> <th>{{ Point.CodeSd }}</th> <th>{{ Point.Id }}</th> <th>{{ Point.Template }}</th> <th>{{ Point.Serveur }}</th> <th>{{ Point.Chemin }}</th> <th>{{ Point.Mini }}</th> <th>{{ Point.Maxi }}</th> <th>{{ Point.Increment }}</th> <th>{{ Point.titre }}</th> <th>{{ Point.Legende }}</th> <th><a href="{{ path('site_point_modifier',{ 'id':Point.Id}) }}" >Modifier</a></th> <th><a href="{{ path('site_point_supprimer', { 'id':Point.Id }) }}">Supprimer</a></th> </tr> {% else %} <li>Pas de code SD</li> {% endfor %} </TABLE> {% endblock %}
-ajouter :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <h3>Formulaire de Modification</h3> <div class="well"> <form method="post" {{ form_enctype(form) }}> {{ form_widget }} <input type="submit" class="btn btn-primary" /> <input type="reset" class="btn_reset" /> <input type='button' name='cancel' value='Annuler' onclick=self.close()> </form> </div>
-mon controleur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <h3>Formulaire de Modification</h3> <div class="well"> <form method="post" {{ form_enctype(form) }}> {{ form_widget }} <input type="submit" class="btn btn-primary" /> <input type="reset" class="btn_reset" /> <input type='button' name='cancel' value='Annuler' onclick=self.close()> </form> </div>
- et mon formulaire :
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 <?php namespace Site\PointBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Site\PointBundle\Entity\Point; use Site\PointBundle\Form\PointType; use Site\PointBundle\Form\RechercherType; use Site\PointBundle\Entity\Rechercher; class DefaultController extends Controller {public function ajouterAction() { $Point=new Point(); $form = $this->createForm(new PointType, $Point); $request=$this->get('request'); if($request->getMethod() == 'POST') { $form->bind($request); if($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($Point); $em->flush(); return $this->redirect($this->generateUrl('site_point_homepage')); } } return $this->render('SitePointBundle:Default:ajouter.html.twig', array('form' => $form->createView(),)); } public function modifierAction() { $Point=new Point(); $form = $this->createForm(new PointType, $Point); $request=$this->get('request'); if($request->getMethod() == 'GET') { $form->bind($request); if($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($Point); $em->flush(); return $this->redirect($this->generateUrl('site_point_voir')); } } return $this->render('SitePointBundle:Default:ajouter.html.twig', array('form' => $form->createView(),)); } }
J'ai vu sur les forums dans mon modifierAction, je devais mettre : $Point = SetPoint($monPoint)
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 <?php namespace Site\PointBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class PointType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('CodeSd','text') ->add('template','choice', array('choices' => array('1'=>'Template 1','2'=>'Template 2','3'=>'Template 3'))) ->add('serveur','text') ->add('chemin','text') ->add('mini','text') ->add('maxi','text') ->add('increment','checkbox',array('label'=> 'mini<maxi','required'=> false,)) ->add('titre','text') ->add('legende','text') ; } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Site\PointBundle\Entity\Point' )); } /** * @return string */ public function getName() { return 'site_pointbundle_point'; } }
mais je ne sais pas comment récupérer $monPoint. Puis je ne sais pas si tout mon code est correct, je suis débutant en Symfony.
Merci d'avance pour l'aide.
Partager