From 63a8a705db38e53e6751ee0209ecdfdf2c4e8b99 Mon Sep 17 00:00:00 2001 From: Romain Guerrero Date: Thu, 6 Dec 2018 14:53:56 +0100 Subject: [PATCH 1/2] add missing use for FormType class --- Form/Extension/FieldTypeHelpExtension.php | 1 + Form/Extension/FieldTypePictoExtension.php | 1 + Form/Extension/FieldTypePopinExtension.php | 1 + 3 files changed, 3 insertions(+) diff --git a/Form/Extension/FieldTypeHelpExtension.php b/Form/Extension/FieldTypeHelpExtension.php index acc34e5..9d14488 100644 --- a/Form/Extension/FieldTypeHelpExtension.php +++ b/Form/Extension/FieldTypeHelpExtension.php @@ -11,6 +11,7 @@ namespace ACSEO\Bundle\DynamicFormBundle\Form\Extension; use Symfony\Component\Form\AbstractTypeExtension; +use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; diff --git a/Form/Extension/FieldTypePictoExtension.php b/Form/Extension/FieldTypePictoExtension.php index 3d97f61..8495f02 100644 --- a/Form/Extension/FieldTypePictoExtension.php +++ b/Form/Extension/FieldTypePictoExtension.php @@ -11,6 +11,7 @@ namespace ACSEO\Bundle\DynamicFormBundle\Form\Extension; use Symfony\Component\Form\AbstractTypeExtension; +use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; diff --git a/Form/Extension/FieldTypePopinExtension.php b/Form/Extension/FieldTypePopinExtension.php index bed04a7..3383647 100644 --- a/Form/Extension/FieldTypePopinExtension.php +++ b/Form/Extension/FieldTypePopinExtension.php @@ -11,6 +11,7 @@ namespace ACSEO\Bundle\DynamicFormBundle\Form\Extension; use Symfony\Component\Form\AbstractTypeExtension; +use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; From a3513e51afd7b9aaf051f469eff1545369a0ec3d Mon Sep 17 00:00:00 2001 From: Romain Guerrero Date: Thu, 10 Jan 2019 15:26:16 +0100 Subject: [PATCH 2/2] convert field types from string value --- Form/Field/FieldBuilder.php | 27 ++++++++++++++++++++++++++- Form/FormManager.php | 7 ++++--- Form/Type/DynamicFormType.php | 2 +- Form/Type/FieldsetType.php | 9 +++++---- Form/Type/VirtualType.php | 7 ++++--- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/Form/Field/FieldBuilder.php b/Form/Field/FieldBuilder.php index 56e93f8..e113e61 100644 --- a/Form/Field/FieldBuilder.php +++ b/Form/Field/FieldBuilder.php @@ -198,7 +198,32 @@ public function addAssociatedFielPostBindEvent($formFields, $builder) */ private function addSingleField($name, $field, $options, $builder) { - $builder->add($name, $field->type, $options); + $builder->add($name, self::getFormTypeClass($field->type), $options); + } + + /** + * @param string $fieldType + */ + public static function getFormTypeClass($fieldType) + { + switch ($fieldType) { + case 'checkbox': + return \Symfony\Component\Form\Extension\Core\Type\CheckboxType::class; + case 'choice': + return \Symfony\Component\Form\Extension\Core\Type\ChoiceType::class; + case 'date': + return \Symfony\Component\Form\Extension\Core\Type\DateType::class; + case 'file': + return \Symfony\Component\Form\Extension\Core\Type\FileType::class; + case 'fieldset': + return \ACSEO\Bundle\DynamicFormBundle\Form\Type\FieldsetType::class; + case 'text': + return \Symfony\Component\Form\Extension\Core\Type\TextType::class; + case 'textarea': + return \Symfony\Component\Form\Extension\Core\Type\TextareaType::class; + } + + return $fieldType; } /** diff --git a/Form/FormManager.php b/Form/FormManager.php index 0579bb3..387e160 100644 --- a/Form/FormManager.php +++ b/Form/FormManager.php @@ -10,11 +10,12 @@ namespace ACSEO\Bundle\DynamicFormBundle\Form; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\Form\FormFactoryInterface; -use ACSEO\Bundle\DynamicFormBundle\Events; use ACSEO\Bundle\DynamicFormBundle\Event\FormEvent; +use ACSEO\Bundle\DynamicFormBundle\Events; use ACSEO\Bundle\DynamicFormBundle\Form\Provider\FormProviderInterface; +use ACSEO\Bundle\DynamicFormBundle\Form\Type\DynamicFormType; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Form\FormFactoryInterface; /** * Class FormManager diff --git a/Form/Type/DynamicFormType.php b/Form/Type/DynamicFormType.php index 66a3d3c..6ce88ca 100644 --- a/Form/Type/DynamicFormType.php +++ b/Form/Type/DynamicFormType.php @@ -48,7 +48,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) { $formFields = json_decode($this->formStruct); - if (!count($formFields)) { + if (!count((array) $formFields)) { throw new Exception('FormStruc is empty'); } diff --git a/Form/Type/FieldsetType.php b/Form/Type/FieldsetType.php index 16611b1..5eb92ba 100644 --- a/Form/Type/FieldsetType.php +++ b/Form/Type/FieldsetType.php @@ -2,11 +2,12 @@ namespace ACSEO\Bundle\DynamicFormBundle\Form\Type; +use ACSEO\Bundle\DynamicFormBundle\Form\Field\FieldBuilder; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; -use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; /** * Class FieldsetType @@ -15,9 +16,9 @@ class FieldsetType extends AbstractType { /** - * @param OptionsResolverInterface $resolver + * {@inheritdoc} */ - public function setDefaultOptions(OptionsResolverInterface $resolver) + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'title' => false, @@ -34,7 +35,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) { if (!empty($options['subforms'])) { foreach ($options['subforms'] as $f) { - $builder->add($f['name'], $f['type'], $f['attr']); + $builder->add($f['name'], FieldBuilder::getFormTypeClass($f['type']), $f['attr']); } } } diff --git a/Form/Type/VirtualType.php b/Form/Type/VirtualType.php index 80ca8a2..063ab2f 100644 --- a/Form/Type/VirtualType.php +++ b/Form/Type/VirtualType.php @@ -10,11 +10,12 @@ namespace ACSEO\Bundle\DynamicFormBundle\Form\Type; +use ACSEO\Bundle\DynamicFormBundle\Form\Field\FieldBuilder; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\OptionsResolver\OptionsResolverInterface; -use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; +use Symfony\Component\OptionsResolver\OptionsResolverInterface; /** * Virtual Type : used for collection form type @@ -45,7 +46,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) unset($this->options['data']); } - $builder->add('value', $this->field->type, $this->options); + $builder->add('value', FieldBuilder::getFormTypeClass($this->field->type), $this->options); } /**