From c7281a5cfcf916085d720db614cfbd8f893ea31f Mon Sep 17 00:00:00 2001 From: Benjamin Dalsass Date: Wed, 10 Jan 2024 09:32:05 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B07063=20-=20Forms=20SDK=20-=20Add=20Symfo?= =?UTF-8?q?ny=20forms=20component?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit error forms issue --- .../FormImplementation/Helper/FormHelper.php | 101 +++++++++++++++++- .../Field/FormFieldTypeEnumeration.php | 5 +- sources/FormSDK/Service/FormFactory.php | 9 +- .../Service/FormFactoryBuilderTrait.php | 16 +++ sources/FormSDK/Symfony/SymfonyBridge.php | 15 ++- .../Symfony/Type/Compound/CollectionType.php | 2 +- .../Symfony/Type/Compound/FieldsetType.php | 2 - .../Symfony/Type/Layout/LayoutType.php | 2 +- templates/formSDK/themes/portal.html.twig | 2 + 9 files changed, 140 insertions(+), 14 deletions(-) diff --git a/sources/FormImplementation/Helper/FormHelper.php b/sources/FormImplementation/Helper/FormHelper.php index 112635536..7e2b95df5 100644 --- a/sources/FormImplementation/Helper/FormHelper.php +++ b/sources/FormImplementation/Helper/FormHelper.php @@ -53,6 +53,7 @@ class FormHelper * @return \Combodo\iTop\FormSDK\Service\FormFactory * @throws \ArchivedObjectException * @throws \CoreException + * @throws \Exception */ static public function CreateSampleFormFactory(FormManager $oFormManager, RouterInterface $oRouter, int $iMode) : FormFactory { @@ -260,6 +261,12 @@ class FormHelper ] ]); + // file - file download + $oFormFactory->AddFileField('file', [ + 'label' => 'Download a file', + 'required' => false + ]); + } if(self::$MODES_DEFINITIONS[$iMode]['layout']){ @@ -273,12 +280,13 @@ class FormHelper ], 'column__2' => [ 'css_classes' => 'custom-container container-color mb-3', - 'fieldset__2' => ['mode', 'interval'], + 'fieldset__1' => ['mode', 'interval'], ], ], 'row__2' => [ 'css_classes' => 'custom-container container-color2 mb-3', + 'fieldset__2' => ['file'], ] ]; @@ -297,4 +305,95 @@ class FormHelper return $oFormFactory; } + + + private function Definitions() + { + // condensé // + lisible - organisé // generic id + $aDescription = [ + + 'row__1' => [ + 'column__1' => [ + 'css_classes' => 'custom-container container-flower layout-grow', + 'fieldset__1' => [ 'birthday', 'city', 'tel'], + ], + 'column__2' => [ + 'css_classes' => 'custom-container container-color mb-3', + 'fieldset__1' => ['mode', 'interval'], + ], + + ], + 'row__2' => [ + 'css_classes' => 'custom-container container-color2 mb-3', + 'fieldset__2' => ['file'], + ] + ]; + + // etendu // - lisible + organisé // free id + $aDescription2 = [ + + '1' => [ + + 'properties' => [ + 'type' => 'row', + 'css_classes' => 'custom-container container-flower layout-grow', + 'rank' => 1 + ], + + 'content' => [ + + '11' => [ + + 'properties' => [ + 'type' => 'column', + 'rank' => 1 + ], + + 'content' => [ + + '111' => [ + + 'properties' => [ + 'type' => 'fieldset', + 'rank' => 1 + ], + + + ], + + '112' => [ + + 'properties' => [ + 'type' => 'fieldset', + 'rank' => 1 + ], + + ] + + ] + ] + + ], + + + + 'column__1' => [ + 'css_classes' => 'custom-container container-flower layout-grow', + 'fieldset__1' => [ 'birthday', 'city', 'tel'], + ], + 'column__2' => [ + 'css_classes' => 'custom-container container-color mb-3', + 'fieldset__1' => ['mode', 'interval'], + ], + + ], + 'row__2' => [ + 'css_classes' => 'custom-container container-color2 mb-3', + 'fieldset__2' => ['file'], + ] + ]; + + return [$aDescription, $aDescription2]; + } + } \ No newline at end of file diff --git a/sources/FormSDK/Field/FormFieldTypeEnumeration.php b/sources/FormSDK/Field/FormFieldTypeEnumeration.php index bd8b400fc..c37ff5538 100644 --- a/sources/FormSDK/Field/FormFieldTypeEnumeration.php +++ b/sources/FormSDK/Field/FormFieldTypeEnumeration.php @@ -36,6 +36,7 @@ enum FormFieldTypeEnumeration case DURATION; case FIELDSET; case COLLECTION; + case FILE; /** * Return available options. @@ -47,7 +48,7 @@ enum FormFieldTypeEnumeration // global options $aOptions = ['required', 'disabled', 'attr', 'label', 'label_attr', 'help']; - // specific options + // : specific options return match ($this) { FormFieldTypeEnumeration::TEXT => array_merge($aOptions, ['constraints'] @@ -86,7 +87,7 @@ enum FormFieldTypeEnumeration // retrieve available options $aAvailableOptions = $this->GetAvailableOptions(); - // check each option... + // check each options... foreach($aOptions as $sKey => $oOption){ if(!in_array($sKey, $aAvailableOptions)){ $aInvalidOptions[] = $sKey; diff --git a/sources/FormSDK/Service/FormFactory.php b/sources/FormSDK/Service/FormFactory.php index 86fd4d3aa..44bcdaba9 100644 --- a/sources/FormSDK/Service/FormFactory.php +++ b/sources/FormSDK/Service/FormFactory.php @@ -35,13 +35,13 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; */ final class FormFactory { - /** @var array $aFieldsDescriptions form types descriptions */ + /** @var array $aFieldsDescriptions form fields descriptions */ private array $aFieldsDescriptions = []; - /** @var mixed $oFieldsData form data */ + /** @var mixed $oFieldsData form fields data */ private mixed $oFieldsData = []; - /** @var array $aLayoutDescription description of the layout */ + /** @var array $aLayoutDescription layout description */ private array $aLayoutDescription = []; /** @var \Combodo\iTop\FormSDK\Service\FactoryAdapter\FormFactoryAdapterInterface[] $aAdapters list of adapters */ @@ -180,8 +180,7 @@ final class FormFactory */ public function CreateForm(?string $sName = null) : mixed { - $aFieldsDescriptions = $this->GetFieldsDescriptions(); - return $this->oSymfonyBridge->CreateForm($aFieldsDescriptions, $this->GetData(), $sName, $this->GetLayoutDescription()); + return $this->oSymfonyBridge->CreateForm($this->GetFieldsDescriptions(), $this->GetData(), $sName, $this->GetLayoutDescription()); } } \ No newline at end of file diff --git a/sources/FormSDK/Service/FormFactoryBuilderTrait.php b/sources/FormSDK/Service/FormFactoryBuilderTrait.php index 241ca5b44..e86e1a80a 100644 --- a/sources/FormSDK/Service/FormFactoryBuilderTrait.php +++ b/sources/FormSDK/Service/FormFactoryBuilderTrait.php @@ -287,4 +287,20 @@ trait FormFactoryBuilderTrait return $this; } + + /** + * Add file field. + * + * @param string $sKey + * @param array $aOptions + * + * @return \Combodo\iTop\FormSDK\Service\FormFactory + * @throws \Exception + */ + public function AddFileField(string $sKey, array $aOptions) : FormFactory + { + $this->aFieldsDescriptions[$sKey] = new FormFieldDescription($sKey, FormFieldTypeEnumeration::FILE, $aOptions); + + return $this; + } } \ No newline at end of file diff --git a/sources/FormSDK/Symfony/SymfonyBridge.php b/sources/FormSDK/Symfony/SymfonyBridge.php index f93e3efcd..a5b2190dc 100644 --- a/sources/FormSDK/Symfony/SymfonyBridge.php +++ b/sources/FormSDK/Symfony/SymfonyBridge.php @@ -30,6 +30,7 @@ use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\DateIntervalType; use Symfony\Component\Form\Extension\Core\Type\DateType; +use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; @@ -135,6 +136,14 @@ class SymfonyBridge 'options' => $aOptions ]; + case FormFieldTypeEnumeration::FILE: + $aOptions = $oFormDescription->GetOptions(); + return [ + 'name' => $oFormDescription->GetName(), + 'type' => FileType::class, + 'options' => $aOptions + ]; + default: return null; } @@ -147,7 +156,8 @@ class SymfonyBridge * * @return void */ - private function TransformFieldsetOptions(array &$aOptions){ + private function TransformFieldsetOptions(array &$aOptions) : void + { $aFields = []; foreach ($aOptions['fields'] as $oChildFormDescription){ @@ -164,7 +174,8 @@ class SymfonyBridge * * @return void */ - private function TransformCollectionOptions(array &$aOptions){ + private function TransformCollectionOptions(array &$aOptions) : void + { $aOptions['entry_type'] = FieldsetType::class; diff --git a/sources/FormSDK/Symfony/Type/Compound/CollectionType.php b/sources/FormSDK/Symfony/Type/Compound/CollectionType.php index 229bbb80c..1faf45fdc 100644 --- a/sources/FormSDK/Symfony/Type/Compound/CollectionType.php +++ b/sources/FormSDK/Symfony/Type/Compound/CollectionType.php @@ -61,6 +61,6 @@ class CollectionType extends AbstractType /** @inheritdoc */ public function getBlockPrefix() { - return 'itop_collection'; + return 'itop_collection'; // need a specific block prefix as we create a CollectionType that's already exist in Symfony namespace } } \ No newline at end of file diff --git a/sources/FormSDK/Symfony/Type/Compound/FieldsetType.php b/sources/FormSDK/Symfony/Type/Compound/FieldsetType.php index 1a4070e3e..b4c7d8450 100644 --- a/sources/FormSDK/Symfony/Type/Compound/FieldsetType.php +++ b/sources/FormSDK/Symfony/Type/Compound/FieldsetType.php @@ -37,8 +37,6 @@ class FieldsetType extends AbstractType /** @inheritdoc */ public function buildForm(FormBuilderInterface $builder, array $options) : void { - - foreach ($options['fields'] as $oField){ $builder->add($oField['name'], $oField['type'], $oField['options']); } diff --git a/sources/FormSDK/Symfony/Type/Layout/LayoutType.php b/sources/FormSDK/Symfony/Type/Layout/LayoutType.php index f2f9ea800..03e9f93f4 100644 --- a/sources/FormSDK/Symfony/Type/Layout/LayoutType.php +++ b/sources/FormSDK/Symfony/Type/Layout/LayoutType.php @@ -47,7 +47,7 @@ class LayoutType extends AbstractType { $resolver->setDefaults([ 'fields' => [], - 'inherit_data' => true // this type is abstract and used for grouping + 'inherit_data' => true // this type is abstract and used for logical grouping ]); } diff --git a/templates/formSDK/themes/portal.html.twig b/templates/formSDK/themes/portal.html.twig index c2dc706de..f902bb6e3 100644 --- a/templates/formSDK/themes/portal.html.twig +++ b/templates/formSDK/themes/portal.html.twig @@ -26,6 +26,8 @@ OQL {% endif %} + + {% endif %}