Bonjour,
J'ai un souci à la récupération des données de mes formulaires ...
Voilà, j'ai un premier formulaire qui contient une liste de formulaires.
Ma page affiche deux formulaires, suivant le formulaire validé, je ne récupère pas les même infos dans mes forms ... Je ne comprends pas vraiment pourquoi ...
Mes différents codes :
FormType Projet:
Formtype Assumptions
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 <?php namespace ATS\CapacityPlanBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class ProjectType extends AbstractType { private $manager; public function __construct(\Doctrine\ORM\EntityManager $manager) { $this->manager = $manager; } public function buildForm(FormBuilderInterface $builder, array $options) { $name = array('label' => 'Project', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 100)))); $builder->add('name', 'text', $name); $engagement = array('label' => 'Engagement', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 100)))); $builder->add('engagement', 'text', $engagement); $engagementManager = array('label' => 'Engagement Manager', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 100)))); $builder->add('engagementManager', 'text', $engagementManager); $projectManager = array('label' => 'Project Manager', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 100)))); $builder->add('projectManager', 'text', $projectManager); $startingWeek = array('label' => 'Starting week', 'constraints' => array(new NotBlank, new Length(array('min' =>1, 'max' => 2)))); $builder->add('startingWeek', 'text', $startingWeek); $version = array('label' => 'Version', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 15)))); $builder->add('version', 'text', $version); $versionDate = array('label' => 'Version Date', 'constraints' => array(new NotBlank)); $builder->add('versionDate', 'date', $versionDate); $weekConfig = array('label' => 'Week config', 'constraints' => array(new NotBlank, new Length(array('min' =>1, 'max' => 2)))); $builder->add('weekConfig', 'text', $weekConfig); $builder->add('assumptions', 'collection', array( // each item in the array will be an "assumption" field 'type' => new AssumptionType($this->manager), 'allow_add' => true, // these options are passed to each "email" type 'options' => array( 'required' => false, 'data_class' => 'ATS\CapacityPlanBundle\Entity\Assumption' ), )); $submitOptions = array('label' => 'Update the project', 'attr'=>array('class'=>'btn')); $builder->add('validate', 'submit', $submitOptions); } public function getDefaultOptions(array $options) { return array( 'data_class' => 'ATS\CapacityPlanBundle\Entity\Project', ); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'ATS\CapacityPlanBundle\Entity\Project', )); } public function getName() { return 'Project'; } }
Dernier FormType Pour créer une Assumption:
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 <?php namespace ATS\CapacityPlanBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use ATS\CapacityPlanBundle\Form\DataTransformer\ProjectToNumberTransformer; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class AssumptionType extends AbstractType { private $manager; public function __construct(\Doctrine\ORM\EntityManager $manager) { $this->manager = $manager; } public function buildForm(FormBuilderInterface $builder, array $options) { $blankAssumption = array('required'=>false, 'attr'=>array('rows'=>1, 'cols'=>60)); $builder->add('text', 'textarea', $blankAssumption); $project = array( // validation message if the data transformer fails 'invalid_message' => 'That is not a valid project number', ); $builder->add('project', 'hidden', $project); $builder->get('project') ->addModelTransformer(new ProjectToNumberTransformer($this->manager)); } public function getDefaultOptions(array $options) { return array( 'data_class' => 'ATS\CapacityPlanBundle\Entity\Assumption', ); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'ATS\CapacityPlanBundle\Entity\Assumption', )); } public function getName() { return 'Assumption'; } }
Mon Action dans controlleur:
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 <?php namespace ATS\CapacityPlanBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use ATS\CapacityPlanBundle\Form\DataTransformer\ProjectToNumberTransformer; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class NewAssumptionType extends AbstractType { private $manager; public function __construct(\Doctrine\ORM\EntityManager $manager) { $this->manager = $manager; } public function buildForm(FormBuilderInterface $builder, array $options) { $blankAssumption = array('required'=>false, 'attr'=>array('rows'=>1, 'cols'=>60)); $builder->add('text', 'textarea', $blankAssumption); $project = array( // validation message if the data transformer fails 'invalid_message' => 'That is not a valid project number', ); $builder->add('project', 'hidden', $project); $builder->get('project') ->addModelTransformer(new ProjectToNumberTransformer($this->manager)); $submitOptions = array('label' => 'Update Assumptions', 'attr'=>array('class'=>'btn')); $builder->add('validate', 'submit', $submitOptions); } public function getDefaultOptions(array $options) { return array( 'data_class' => 'ATS\CapacityPlanBundle\Entity\Assumption', ); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'ATS\CapacityPlanBundle\Entity\Assumption', )); } public function getName() { return 'Assumption'; } }
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 public function viewAction(Request $request, $id) { $em = $this->getDoctrine()->getManager(); $rep = $em->getRepository('ATSCapacityPlanBundle:Project'); $project = $rep->find($id); // Get forms to display Config $formProjectToDisplay = $this->createForm(new ProjectType($em), $project); $formSelectAnotherProject = $this->getFormSelectProject(); $formAddAssumption = $this->createForm(new NewAssumptionType($em)); //$newAss = $this->createForm(new NewAssumptionType($em), $newAssumption); $route = 'ats_capacity_plan_configurationview'; $formAddAssumption->handleRequest($request); $formProjectToDisplay->handleRequest($request); $formSelectAnotherProject->handleRequest($request); if($formSelectAnotherProject->isValid()){ if ($formSelectAnotherProject->getClickedButton()->getName() == 'display')// User has clicked on submit and a project is selected { $projectSelected = $formSelectAnotherProject->get('projects')->getData(); $projectId = $projectSelected->getId(); // execute action to display config return $this->redirect($this->generateUrl($route, array('id'=>$projectId))); } } if($formProjectToDisplay->isValid())// User has clicked on submit and datas are correct { if ($formProjectToDisplay->getClickedButton()->getName() == 'validate'){ foreach ($formProjectToDisplay->get('assumptions') as $value) { $value->handleRequest($request); var_dump($value->getData()->getText()); }exit; $this->updateProjectInformation($formProjectToDisplay->getData()); // Persist infos in DB return $this->redirect($this->generateUrl($route, array('id'=>$id))); } } if($formAddAssumption->isValid())// User has clicked on submit and datas are correct { foreach ($formProjectToDisplay->get('assumptions') as $value) { $value->handleRequest($request); var_dump($value->getData()->getText()); }exit; if (!is_null($formAddAssumption->getData()->getText()) ) { $formAddAssumption->getData()->setProject($project); $project->getAssumptions()->add($newAssumption); } $this->updateProjectInformation($project); // Persist infos in DB return $this->redirect($this->generateUrl($route, array('id'=>$project->getId()))); } $args = array( 'addNewAssumption' => $formAddAssumption->createView(), 'formProjectSelected' => $formProjectToDisplay->createView(), 'formChangeProject' => $formSelectAnotherProject->createView(), 'confirm' => $this->confirm, ); return $this->render('ATSCapacityPlanBundle:Configuration:ConfigurationView.html.twig', $args); }
Ici, si j'ai admettons trois assumptions dans mon projet, j'ai donc trois form assumptions, la première à bien sa valeur, mais les deux autres sont à null...
Ici, toute sles valeurs récupérées sont correctes ...
Je ne comprends pas ... Merci beaucoup d'avance![]()
Partager