N°7063 - Forms SDK - Add Symfony forms component

error forms issue
This commit is contained in:
Benjamin Dalsass
2024-01-09 12:42:48 +01:00
parent 153027434d
commit 5213e205af
12 changed files with 192 additions and 40 deletions

View File

@@ -413,10 +413,10 @@ return array(
'Combodo\\iTop\\FormSDK\\Service\\FormFactoryBuilderTrait' => $baseDir . '/sources/FormSDK/Service/FormFactoryBuilderTrait.php',
'Combodo\\iTop\\FormSDK\\Service\\FormManager' => $baseDir . '/sources/FormSDK/Service/FormManager.php',
'Combodo\\iTop\\FormSDK\\Symfony\\SymfonyBridge' => $baseDir . '/sources/FormSDK/Symfony/SymfonyBridge.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Compound\\CollectionType' => $baseDir . '/sources/FormSDK/Symfony/Type/Compound/CollectionType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Compound\\FieldsetType' => $baseDir . '/sources/FormSDK/Symfony/Type/Compound/FieldsetType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Compound\\FormObjectType' => $baseDir . '/sources/FormSDK/Symfony/Type/Compound/FormObjectType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Layout\\AbstractLayoutType' => $baseDir . '/sources/FormSDK/Symfony/Type/Layout/LayoutType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Layout\\ColumnType' => $baseDir . '/sources/FormSDK/Symfony/Type/Layout/ColumnType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Layout\\LayoutType' => $baseDir . '/sources/FormSDK/Symfony/Type/Layout/LayoutType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Layout\\RowType' => $baseDir . '/sources/FormSDK/Symfony/Type/Layout/RowType.php',
'Combodo\\iTop\\Form\\Field\\AbstractSimpleField' => $baseDir . '/sources/Form/Field/AbstractSimpleField.php',
'Combodo\\iTop\\Form\\Field\\BlobField' => $baseDir . '/sources/Form/Field/BlobField.php',

View File

@@ -822,10 +822,10 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
'Combodo\\iTop\\FormSDK\\Service\\FormFactoryBuilderTrait' => __DIR__ . '/../..' . '/sources/FormSDK/Service/FormFactoryBuilderTrait.php',
'Combodo\\iTop\\FormSDK\\Service\\FormManager' => __DIR__ . '/../..' . '/sources/FormSDK/Service/FormManager.php',
'Combodo\\iTop\\FormSDK\\Symfony\\SymfonyBridge' => __DIR__ . '/../..' . '/sources/FormSDK/Symfony/SymfonyBridge.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Compound\\CollectionType' => __DIR__ . '/../..' . '/sources/FormSDK/Symfony/Type/Compound/CollectionType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Compound\\FieldsetType' => __DIR__ . '/../..' . '/sources/FormSDK/Symfony/Type/Compound/FieldsetType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Compound\\FormObjectType' => __DIR__ . '/../..' . '/sources/FormSDK/Symfony/Type/Compound/FormObjectType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Layout\\AbstractLayoutType' => __DIR__ . '/../..' . '/sources/FormSDK/Symfony/Type/Layout/LayoutType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Layout\\ColumnType' => __DIR__ . '/../..' . '/sources/FormSDK/Symfony/Type/Layout/ColumnType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Layout\\LayoutType' => __DIR__ . '/../..' . '/sources/FormSDK/Symfony/Type/Layout/LayoutType.php',
'Combodo\\iTop\\FormSDK\\Symfony\\Type\\Layout\\RowType' => __DIR__ . '/../..' . '/sources/FormSDK/Symfony/Type/Layout/RowType.php',
'Combodo\\iTop\\Form\\Field\\AbstractSimpleField' => __DIR__ . '/../..' . '/sources/Form/Field/AbstractSimpleField.php',
'Combodo\\iTop\\Form\\Field\\BlobField' => __DIR__ . '/../..' . '/sources/Form/Field/BlobField.php',

View File

@@ -3,7 +3,7 @@
'name' => 'combodo/itop',
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '00f241e4cf314d4190bedeba80de9ec12461b958',
'reference' => '153027434d1a860bbb424fdbf1bd6c7e47217e27',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
@@ -22,7 +22,7 @@
'combodo/itop' => array(
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '00f241e4cf314d4190bedeba80de9ec12461b958',
'reference' => '153027434d1a860bbb424fdbf1bd6c7e47217e27',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),

View File

@@ -72,7 +72,24 @@ class FormHelper
'notify' => true,
'language' => 'FR FR',
'mode' => '1',
'options' => ['0', '2','4']
'options' => ['0', '2', '4'],
'collection' => [
[
'text1' => 'Benjamin',
'text2' => 'DALSASS',
'date1' => new DateTime('1979/06/27')
],
[
'text1' => 'Nelly',
'text2' => 'DALSASS',
'date1' => new DateTime('1977/04/6')
],
[
'text1' => 'Léonard',
'text2' => 'BASTID',
'date1' => new DateTime('1975/03/16')
]
]
];
$oFormFactory->SetData($aData);
@@ -225,6 +242,24 @@ class FormHelper
]
]);
// options - select with static data
$oText1 = new FormFieldDescription('text1', FormFieldTypeEnumeration::TEXT, []);
$oText2 = new FormFieldDescription('text2', FormFieldTypeEnumeration::TEXT, []);
$oDate = new FormFieldDescription('date1', FormFieldTypeEnumeration::DATE, [
'widget' => 'single_text'
]);
$oFormFactory->AddCollectionField('collection', [
'label' => 'Une Collection',
'element_type' => FormFieldTypeEnumeration::FIELDSET,
'fields_labels' => ['Prénom', 'Nom', 'Naissance'],
'element_options' => [
'fields' => [
'text1' => $oText1,
'text2' => $oText2,
'date1' => $oDate]
]
]);
}
if(self::$MODES_DEFINITIONS[$iMode]['layout']){

View File

@@ -25,16 +25,17 @@ namespace Combodo\iTop\FormSDK\Field;
* @package FormSDK
* @since 3.2.0
*/
enum FormFieldTypeEnumeration : string
enum FormFieldTypeEnumeration
{
case TEXT = 'TEXT';
case NUMBER = 'NUMBER';
case AREA = 'AREA';
case DATE = 'DATE';
case SELECT = 'SELECT';
case SWITCH = 'SWITCH';
case DURATION = 'DURATION';
case FIELDSET = 'FIELDSET';
case TEXT;
case NUMBER;
case AREA;
case DATE;
case SELECT;
case SWITCH;
case DURATION;
case FIELDSET;
case COLLECTION;
/**
* Return available options.
@@ -63,6 +64,9 @@ enum FormFieldTypeEnumeration : string
FormFieldTypeEnumeration::FIELDSET => array_merge($aOptions,
['fields', 'layout']
),
FormFieldTypeEnumeration::COLLECTION => array_merge($aOptions,
['element_type', 'element_options', 'fields_labels']
),
default => $aOptions,
};
}

View File

@@ -184,12 +184,8 @@ final class FormFactoryObjectAdapter implements FormFactoryAdapterInterface
'fields' => $aFieldsDescriptions,
'layout' => [
'row__1' => [
'column__1' => [
'fieldset__1' => [ 'name'],
],
'column__2' => [
'fieldset__2' => ['mobile_phone'],
],
'column__1' => [ 'name'],
'column__2' => ['mobile_phone'],
],
]
]);

View File

@@ -246,4 +246,20 @@ trait FormFactoryBuilderTrait
return $this;
}
/**
* Add collection field.
*
* @param string $sKey
* @param array $aOptions
*
* @return \Combodo\iTop\FormSDK\Service\FormFactory
* @throws \Exception
*/
public function AddCollectionField(string $sKey, array $aOptions) : FormFactory
{
$this->aFieldsDescriptions[$sKey] = new FormFieldDescription($sKey, FormFieldTypeEnumeration::COLLECTION, $aOptions);
return $this;
}
}

View File

@@ -21,6 +21,7 @@ namespace Combodo\iTop\FormSDK\Symfony;
use Combodo\iTop\FormSDK\Field\FormFieldDescription;
use Combodo\iTop\FormSDK\Field\FormFieldTypeEnumeration;
use Combodo\iTop\FormSDK\Symfony\Type\Compound\CollectionType;
use Combodo\iTop\FormSDK\Symfony\Type\Compound\FieldsetType;
use Combodo\iTop\FormSDK\Symfony\Type\Layout\ColumnType;
use Combodo\iTop\FormSDK\Symfony\Type\Layout\RowType;
@@ -103,12 +104,7 @@ class SymfonyBridge
case FormFieldTypeEnumeration::FIELDSET:
$aOptions = $oFormDescription->GetOptions();
$aFields = [];
foreach ($aOptions['fields'] as $oChildFormDescription){
$aSymfony = $this->ToSymfonyFormType($oChildFormDescription);
$aFields[$oChildFormDescription->GetName()] = $aSymfony;
}
$aOptions['fields'] = $aFields;
$this->TransformFieldsetOptions($aOptions);
return [
'name' => $oFormDescription->GetName(),
'type' => FieldsetType::class,
@@ -129,11 +125,41 @@ class SymfonyBridge
'options' => $oFormDescription->GetOptions()
];
case FormFieldTypeEnumeration::COLLECTION:
$aOptions = $oFormDescription->GetOptions();
$this->TransformCollectionOptions($aOptions);
return [
'name' => $oFormDescription->GetName(),
'type' => CollectionType::class,
'options' => $aOptions
];
default:
return null;
}
}
private function TransformFieldsetOptions(array &$aOptions){
$aFields = [];
foreach ($aOptions['fields'] as $oChildFormDescription){
$aSymfony = $this->ToSymfonyFormType($oChildFormDescription);
$aFields[$oChildFormDescription->GetName()] = $aSymfony;
}
$aOptions['fields'] = $aFields;
}
private function TransformCollectionOptions(array &$aOptions){
$aOptions['entry_type'] = FieldsetType::class;
$this->TransformFieldsetOptions($aOptions['element_options']);
$aOptions['entry_options'] = $aOptions['element_options'];
unset($aOptions['element_options']);
unset($aOptions['element_type']);
}
/**
* Create Symfony form.
*

View File

@@ -0,0 +1,66 @@
<?php
/*
* Copyright (C) 2013-2023 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\FormSDK\Symfony\Type\Compound;
use Combodo\iTop\FormSDK\Field\FormFieldDescription;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Util\StringUtil;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Type representing a collection;
*
* @package FormSDK
* @since 3.2.0
*/
class CollectionType extends AbstractType
{
/** @inheritdoc */
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'fields_labels' => [],
]);
}
/** @inheritdoc */
public function getParent(): string
{
return \Symfony\Component\Form\Extension\Core\Type\CollectionType::class;
}
/** @inheritdoc */
public function buildView(FormView $view, FormInterface $form, array $options): void
{
// pass the form type option directly to the template
$view->vars['fields_labels'] = $options['fields_labels'];
}
/** @inheritdoc */
public function getBlockPrefix()
{
return 'itop_collection';
}
}

View File

@@ -34,8 +34,6 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class FieldsetType extends AbstractType
{
/** @inheritdoc */
public function buildForm(FormBuilderInterface $builder, array $options) : void
{
@@ -46,20 +44,11 @@ class FieldsetType extends AbstractType
}
}
private function handleRow(FormBuilderInterface $builder, array $aData){
}
private function handleColumn(FormBuilderInterface $builder, array $aData){
}
/** @inheritdoc */
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'fields' => [],
'view' => [],
'layout' => []
]);
}

View File

@@ -37,7 +37,6 @@ class LayoutType extends AbstractType
/** @inheritdoc */
public function buildForm(FormBuilderInterface $builder, array $options) : void
{
foreach ($options['fields'] as $oField){
$builder->add($oField['name'], $oField['type'], $oField['options']);
}

View File

@@ -42,3 +42,24 @@
{%- block fieldset_row -%}
{{ form_row(form, {'attr' : {'class' : attr.class|default('') ~ ' form-compound-fieldset'}}) }}
{%- endblock fieldset_row -%}
{%- block itop_collection_widget -%}
<table class="table table-striped">
<thead>
<tr>
{% for label in form.vars.fields_labels %}
<td>{{ label }}</td>
{% endfor %}
</tr>
</thead>
<tbody>
{% for el in form %}
<tr>
{% for child in el.children %}
<td>{{ form(child) }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{%- endblock itop_collection_widget -%}