SDK Form demonstrator Use iTop FormBuilder in the controller to allow transparent use of dynamic forms

This commit is contained in:
Eric Espie
2025-04-18 09:39:19 +02:00
parent 2873570677
commit 3d72800755
5 changed files with 112 additions and 6 deletions

View File

@@ -26,6 +26,7 @@ use Combodo\iTop\Application\WebPage\ErrorPage;
use Combodo\iTop\Application\WebPage\iTopWebPage;
use Combodo\iTop\Application\WebPage\WebPage;
use Combodo\iTop\Controller\AbstractController;
use Combodo\iTop\Forms\Forms;
use Dict;
use Exception;
use ExecutionKPI;
@@ -38,11 +39,9 @@ use SetupUtils;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\Forms;
use Symfony\Component\HttpFoundation\Request;
use Twig\Error\Error;
use Twig\Error\SyntaxError;
@@ -689,10 +688,7 @@ abstract class Controller extends AbstractController
public function GetFormBuilder(string $type = FormType::class, mixed $data = null, array $options = []): FormBuilderInterface
{
$oFormFactory = Forms::createFormFactoryBuilder()
->addExtension(new HttpFoundationExtension())
->getFormFactory();
return $oFormFactory->createBuilder($type, $data,$options);
return Forms::createFormFactory()->createBuilder($type, $data,$options);
}
public function GetForm(string $type = FormType::class, mixed $data = null, array $options = []): FormInterface

View File

@@ -0,0 +1,12 @@
<?php
/*
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Forms\FormType\Base;
interface DynamicFormInterface
{
}

43
sources/Forms/Forms.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
/*
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Forms;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\FormFactoryBuilder;
use Symfony\Component\Form\FormFactoryBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
/**
* Plumbing for iTop custom form builder.
*/
final class Forms
{
/**
* Creates a form factory with the iTop configuration.
*/
public static function createFormFactory(): FormFactoryInterface
{
return self::createFormFactoryBuilder()->getFormFactory();
}
/**
* Creates a form factory builder with the iTop configuration.
*/
public static function createFormFactoryBuilder(): FormFactoryBuilderInterface
{
return (new FormFactoryBuilder())
->addExtension(new HttpFoundationExtension())
->setResolvedTypeFactory(new ResolvedFormTypeFactory());
}
/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Forms;
use Combodo\iTop\Forms\FormType\Base\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\ResolvedFormType as SymfonyResolvedFormType;
use Symfony\Component\Form\ResolvedFormTypeInterface;
/**
* Plumbing for iTop custom form builder.
*/
class ResolvedFormType extends SymfonyResolvedFormType implements ResolvedFormTypeInterface
{
/**
* Creates a new builder instance.
*
* Override this method if you want to customize the builder class.
*/
protected function newBuilder(string $name, ?string $dataClass, FormFactoryInterface $factory, array $options): FormBuilderInterface
{
$builder = parent::newBuilder($name, $dataClass, $factory, $options);
// Wrap the builder in a DynamicFormBuilder
return new FormBuilder($builder);
}
}

View File

@@ -0,0 +1,22 @@
<?php
/*
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Forms;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\ResolvedFormTypeFactoryInterface;
use Symfony\Component\Form\ResolvedFormTypeInterface;
/**
* Plumbing for iTop custom form builder.
*/
class ResolvedFormTypeFactory implements ResolvedFormTypeFactoryInterface
{
public function createResolvedType(FormTypeInterface $type, array $typeExtensions, ?ResolvedFormTypeInterface $parent = null): ResolvedFormTypeInterface
{
return new ResolvedFormType($type, $typeExtensions, $parent);
}
}