|
| 1 | +diff --git a/src/AppBundle/Form/GenusFormType.php b/src/AppBundle/Form/GenusFormType.php |
| 2 | +index b27928a..cdeb7b7 100644 |
| 3 | +--- a/src/AppBundle/Form/GenusFormType.php |
| 4 | ++++ b/src/AppBundle/Form/GenusFormType.php |
| 5 | +@@ -12,6 +12,8 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
| 6 | + use Symfony\Component\Form\Extension\Core\Type\CollectionType; |
| 7 | + use Symfony\Component\Form\Extension\Core\Type\DateType; |
| 8 | + use Symfony\Component\Form\FormBuilderInterface; |
| 9 | ++use Symfony\Component\Form\FormEvent; |
| 10 | ++use Symfony\Component\Form\FormEvents; |
| 11 | + use Symfony\Component\Form\FormInterface; |
| 12 | + use Symfony\Component\Form\FormView; |
| 13 | + use Symfony\Component\OptionsResolver\OptionsResolver; |
| 14 | +@@ -51,6 +53,8 @@ class GenusFormType extends AbstractType |
| 15 | + 'by_reference' => false, |
| 16 | + ]) |
| 17 | + ; |
| 18 | ++ |
| 19 | ++ $builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit')); |
| 20 | + } |
| 21 | + |
| 22 | + public function configureOptions(OptionsResolver $resolver) |
| 23 | +@@ -59,4 +63,45 @@ class GenusFormType extends AbstractType |
| 24 | + 'data_class' => 'AppBundle\Entity\Genus' |
| 25 | + ]); |
| 26 | + } |
| 27 | ++ |
| 28 | ++ /** |
| 29 | ++ * This fixes a validation issue with the Collection. Suppose |
| 30 | +
10000
span>+ * the following situation: |
| 31 | ++ * |
| 32 | ++ * A) Edit a Genus |
| 33 | ++ * B) Add 2 new scientists - don't submit & leave all fields blank |
| 34 | ++ * C) Delete the FIRST scientist |
| 35 | ++ * D) Submit the form |
| 36 | ++ * |
| 37 | ++ * The one new scientist has a validation error, because |
| 38 | ++ * the yearsStudied field was left blank. But, this error |
| 39 | ++ * shows at the *top* of the form, not attached to the form. |
| 40 | ++ * The reason is that, on submit, addGenusScientist() is |
| 41 | ++ * called, and the new scientist is added to the next available |
| 42 | ++ * index (so, if the Genus previously had 2 scientists, the |
| 43 | ++ * new GenusScientist is added to the "2" index). However, |
| 44 | ++ * in the HTML before the form was submitted, the index used |
| 45 | ++ * in the name attribute of the fields for the new scientist |
| 46 | ++ * was *3*: 0 & 1 were used for the existing scientists and 2 was |
| 47 | ++ * used for the first genus scientist form that you added |
| 48 | ++ * (and then later deleted). This mis-match confuses the validator, |
| 49 | ++ * which thinks there is an error on genusScientists[2].yearsStudied, |
| 50 | ++ * and fails to map that to the genusScientists[3].yearsStudied |
| 51 | ++ * field. |
| 52 | ++ * |
| 53 | ++ * Phew! It's a big pain :). Below, we fix it! On submit, |
| 54 | ++ * we simply re-index the submitted data before it's bound |
| 55 | ++ * to the form. The submitted genusScientists data, which |
| 56 | ++ * previously had index 0, 1 and 3, will now have indexes |
| 57 | ++ * 0, 1 and 2. And these indexes will match the indexes |
| 58 | ++ * that they have on the Genus.genusScientists property. |
| 59 | ++ * |
| 60 | ++ * @param FormEvent $event |
| 61 | ++ */ |
| 62 | ++ public function onPreSubmit(FormEvent $event) |
| 63 | ++ { |
| 64 | ++ $data = $event->getData(); |
| 65 | ++ $data['genusScientists'] = array_values($data['genusScientists']); |
| 66 | ++ $event->setData($data); |
| 67 | ++ } |
| 68 | + } |
0 commit comments