From 7137a64d6d58a1a6fe95aeef807e7804938d5a73 Mon Sep 17 00:00:00 2001 From: Ross Keatinge Date: Sat, 15 Apr 2017 18:43:43 -0400 Subject: [PATCH 1/5] Fix resubmit for new tagged entities. --- Form/Type/Select2EntityType.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index b9d2476..70179ea 100644 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -10,6 +10,7 @@ use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\Routing\RouterInterface; use Tetranz\Select2EntityBundle\Form\DataTransformer\EntitiesToPropertyTransformer; use Tetranz\Select2EntityBundle\Form\DataTransformer\EntityToPropertyTransformer; @@ -107,6 +108,34 @@ public function finishView(FormView $view, FormInterface $form, array $options) } } + if ($view->vars['allow_add']['enabled'] && $form->isSubmitted()) { + // Validation failed. Rebuild value(s) for new entities created by tag. + + // The post creates a value with a blank key. + unset($view->vars['value']['']); + + // entities as iterable collection or array. + $entities = $options['multiple'] ? $form->getData() : [$form->getData()]; + + $accessor = PropertyAccess::createPropertyAccessor(); + $textProperty = isset($options['text_property']) ? $options['text_property'] : null; + $newTagPrefix = $view->vars['allow_add']['new_tag_prefix']; + + foreach ($entities as $entity) { + if ($this->em->contains($entity)) { + // Existing entity. + continue; + } + + // New entity so add a value with key prefix + text and value text. + $text = is_null($textProperty) + ? $value = (string) $entity + : $accessor->getValue($entity, $textProperty); + + $view->vars['value'][$newTagPrefix . $text] = $text; + } + } + if ($options['multiple']) { $view->vars['full_name'] .= '[]'; } From f6650619e0013728d93c93975d7858943471e310 Mon Sep 17 00:00:00 2001 From: Ross Keatinge Date: Sat, 15 Apr 2017 18:45:06 -0400 Subject: [PATCH 2/5] Comment. --- Form/Type/Select2EntityType.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index 70179ea..0d67c86 100644 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -109,7 +109,8 @@ public function finishView(FormView $view, FormInterface $form, array $options) } if ($view->vars['allow_add']['enabled'] && $form->isSubmitted()) { - // Validation failed. Rebuild value(s) for new entities created by tag. + // Form is being displayed again after a submit. + // Rebuild value(s) for new entities created by tag. // The post creates a value with a blank key. unset($view->vars['value']['']); From 6558c799b8791e49df120312a0e99bf52cad054b Mon Sep 17 00:00:00 2001 From: Ross Keatinge Date: Sun, 16 Apr 2017 09:32:13 -0400 Subject: [PATCH 3/5] Rebuild ->vars['value'] --- Form/Type/Select2EntityType.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index 0d67c86..926e7d0 100644 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -109,31 +109,33 @@ public function finishView(FormView $view, FormInterface $form, array $options) } if ($view->vars['allow_add']['enabled'] && $form->isSubmitted()) { - // Form is being displayed again after a submit. - // Rebuild value(s) for new entities created by tag. + // Form is being displayed again after a submit that failed validation. + // $view->vars['value'] needs to be rebuilt to handle new entities added with a tag. - // The post creates a value with a blank key. - unset($view->vars['value']['']); - - // entities as iterable collection or array. + // get entities as iterable collection or array. $entities = $options['multiple'] ? $form->getData() : [$form->getData()]; $accessor = PropertyAccess::createPropertyAccessor(); $textProperty = isset($options['text_property']) ? $options['text_property'] : null; $newTagPrefix = $view->vars['allow_add']['new_tag_prefix']; - foreach ($entities as $entity) { - if ($this->em->contains($entity)) { - // Existing entity. - continue; - } + $view->vars['value'] = []; - // New entity so add a value with key prefix + text and value text. + foreach ($entities as $entity) { + // Get text value $text = is_null($textProperty) ? $value = (string) $entity : $accessor->getValue($entity, $textProperty); - $view->vars['value'][$newTagPrefix . $text] = $text; + // Get primary key + $primaryKey = $accessor->getValue($entity, $options['primary_key']); + if (is_null($primaryKey)) { + // This is not persisted and does not have a primary key + // so make the key = prefix + text. + $primaryKey = $newTagPrefix . $text; + } + + $view->vars['value'][$primaryKey] = $text; } } From 27c1b64dc13a1755fbdfa79bc748021c430e69c0 Mon Sep 17 00:00:00 2001 From: Ross Keatinge Date: Sun, 16 Apr 2017 14:40:01 -0400 Subject: [PATCH 4/5] Getting the logic right. --- Form/Type/Select2EntityType.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index 926e7d0..3d2ae30 100644 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -124,18 +124,20 @@ public function finishView(FormView $view, FormInterface $form, array $options) foreach ($entities as $entity) { // Get text value $text = is_null($textProperty) - ? $value = (string) $entity + ? (string) $entity : $accessor->getValue($entity, $textProperty); - // Get primary key - $primaryKey = $accessor->getValue($entity, $options['primary_key']); - if (is_null($primaryKey)) { - // This is not persisted and does not have a primary key - // so make the key = prefix + text. - $primaryKey = $newTagPrefix . $text; + // Get choice field (primary key). + $choiceFieldValue = $accessor->getValue($entity, $options['primary_key']); + + if (!$this->em->contains($entity)) { + // This is a new entity, added via a tag, not persisted yet. + // A new entity may or may not already have a non-null choice field. + // If the new entity already has a choice field, use it, otherwise use the text field. + $choiceFieldValue = $newTagPrefix . ($choiceFieldValue ? : $text); } - $view->vars['value'][$primaryKey] = $text; + $view->vars['value'][$choiceFieldValue] = $text; } } From 8a1f9e9ffa0e6762746993928a9585d6535a9a83 Mon Sep 17 00:00:00 2001 From: Ross Keatinge Date: Sun, 16 Apr 2017 19:05:18 -0400 Subject: [PATCH 5/5] Add new tag text. --- Form/Type/Select2EntityType.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index 3d2ae30..b338a16 100644 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -135,6 +135,8 @@ public function finishView(FormView $view, FormInterface $form, array $options) // A new entity may or may not already have a non-null choice field. // If the new entity already has a choice field, use it, otherwise use the text field. $choiceFieldValue = $newTagPrefix . ($choiceFieldValue ? : $text); + + $text .= $view->vars['allow_add']['new_tag_text']; } $view->vars['value'][$choiceFieldValue] = $text;