Salut et bonne année !
Bon je recommence l'année de façon très compliqué et avec l'impression que je ne vais jamais m'en sortir ...
J'ai voulu éclaircir mon code car effectivement ça devenait compliqué ... du coup j'ai externalisé les form builders et voulu suivre tes conseils du derniers message (Un formType dédié à l'objet Assumption dans lequel on aura un champs hidden contenant le project_id / Un autre formType dédié à l'objet Project qui lui embarquera une 'collection' de AssumptionType).
Désormais j'ai encore un message d'erreur qui me fait tourner en rond ... :
The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class ATS\CapacityPlanBundle\Entity\Assumption. You can avoid this error by setting the "data_class" option to "ATS\CapacityPlanBundle\Entity\Assumption" or by adding a view transformer that transforms an instance of class ATS\CapacityPlanBundle\Entity\Assumption to scalar, array or an instance of \ArrayAccess.
Je ne comprends pas pourquoi il me met ça, dans mon constructeur de 'Projet' je dit bien que Assumptions doit etre de type ArrayCollection...
Tu y verra surement plus clair que moi :oops:
Je te montre mes classes :
Assumption Type
Project TypeCode:
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 <?php namespace ATS\CapacityPlanBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use ATS\CapacityPlanBundle\Form\DataTransformer\ProjectToNumberTransformer; 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 getName() { return 'Assumption'; } }
ControllerCode:
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 <?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; 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); $submitOptions = array('label' => 'Add the project', 'attr'=>array('class'=>'btn')); $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 ), )); $builder->add('validate', 'submit', $submitOptions); } public function getDefaultOptions(array $options) { return array( 'data_class' => 'ATS\CapacityPlanBundle\Entity\Project', ); } public function getName() { return 'Project'; } }
Désolé d'encore te déranger ... Ce projet me fait un peu déprimer ... Mais je suis sur que passé ces problème, si je pige le truc ça devrait aller tout seul après ...:roll:Code:
1
2
3
4
5
6
7
8
9
10
11 $em = $this->getDoctrine()->getManager(); $rep = $em->getRepository('ATSCapacityPlanBundle:Project'); $session->set('project', $rep->find($id)); $project = $session->get('project'); //} // Get forms to display Config $formProjectToDisplay = $this->createForm(new ProjectType($em), $project); foreach ($assumptions as $assumption) { $formAssumptionsToDisplay[] = $this->createForm(new AssumptionType($em), $assumption); }
Merci d'avance