mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-22 18:18:46 +02:00
SDK Form demonstrator wip sub-forms
This commit is contained in:
40
sources/Forms/FormType/Attribute/ExternalKeyType.php
Normal file
40
sources/Forms/FormType/Attribute/ExternalKeyType.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/*
|
||||
* @copyright Copyright (C) 2010-2025 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Forms\FormType\Attribute;
|
||||
|
||||
use Combodo\iTop\Forms\FormType\Base\AbstractType;
|
||||
use Combodo\iTop\Forms\FormType\FormTypeException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use ValueSetObjects;
|
||||
|
||||
class ExternalKeyType extends AbstractType
|
||||
{
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
parent::configureOptions($resolver);
|
||||
$resolver->setRequired('class');
|
||||
$resolver->setAllowedTypes('class', 'string');
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$oModelReflection = new \ModelReflectionRuntime();
|
||||
$sClass = $options['class'];
|
||||
if (!$oModelReflection->IsValidClass($sClass)) {
|
||||
throw new FormTypeException("Unknown class $sClass");
|
||||
}
|
||||
$oValueSet = new ValueSetObjects("SELECT `$sClass`");
|
||||
$aUserOptions['choices'] = array_flip($oValueSet->GetValues([]));
|
||||
$aUserOptions['multiple'] = false;
|
||||
$aUserOptions['inherit_data'] = true;
|
||||
$builder->add('selected', SymfonyChoiceType::class, $aUserOptions);
|
||||
parent::buildForm($builder, $options);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,6 +33,11 @@ abstract class AbstractType extends SymfonyAbstractType
|
||||
->setAllowedTypes('dynamic_form_hook.event_name', 'string');
|
||||
}
|
||||
|
||||
public function ConfigureDynamicOptions(OptionsResolver $oResolver)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called only when GetPrerequisites() is not null
|
||||
* @param array $aUserOptions
|
||||
|
||||
@@ -80,9 +80,11 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
$oForm->add($sName, HiddenType::class, ['mapped' => false]);
|
||||
}
|
||||
|
||||
public function Finalize(): void
|
||||
protected function Finalize(): void
|
||||
{
|
||||
\IssueLog::Info($this->oDependencies);
|
||||
if ($this->oDependencies->HasChildren()) {
|
||||
\IssueLog::Info($this->oDependencies);
|
||||
}
|
||||
|
||||
foreach ($this->oDependencies as $oField) {
|
||||
if ($oField->HasChildren()) {
|
||||
@@ -103,8 +105,11 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
|
||||
public function add($child, ?string $type = null, array $options = []): static
|
||||
{
|
||||
if (!is_subclass_of($type, AbstractType::class)) {
|
||||
throw new \Exception("type must be an instance of AbstractType (found $type)");
|
||||
$this->oDependencies->Add($child, $type);
|
||||
$this->builder->add($child, $type, $options);
|
||||
return $this;
|
||||
}
|
||||
|
||||
$oType = new $type();
|
||||
$aPrerequisites = $oType->GetPrerequisites($options);
|
||||
if (is_null($aPrerequisites)) {
|
||||
|
||||
12
sources/Forms/FormType/FormTypeException.php
Normal file
12
sources/Forms/FormType/FormTypeException.php
Normal 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;
|
||||
|
||||
class FormTypeException extends \Exception
|
||||
{
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use Combodo\iTop\Forms\FormType\Base\AbstractType;
|
||||
use Dict;
|
||||
use Exception;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class AttCodeGroupByType extends AbstractType
|
||||
{
|
||||
@@ -38,6 +39,12 @@ class AttCodeGroupByType extends AbstractType
|
||||
return $aFormOptions;
|
||||
}
|
||||
|
||||
public function ConfigureDynamicOptions(OptionsResolver $oResolver)
|
||||
{
|
||||
$oResolver->setRequired('source_class');
|
||||
$oResolver->setAllowedTypes('source_class', 'string');
|
||||
}
|
||||
|
||||
public function GetPrerequisites(array $aUserOptions): ?array
|
||||
{
|
||||
return [$aUserOptions['source_class']];
|
||||
|
||||
@@ -11,6 +11,7 @@ use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\ResolvedFormType as SymfonyResolvedFormType;
|
||||
use Symfony\Component\Form\ResolvedFormTypeInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
|
||||
/**
|
||||
@@ -18,6 +19,8 @@ use Symfony\Component\Form\ResolvedFormTypeInterface;
|
||||
*/
|
||||
class ResolvedFormType extends SymfonyResolvedFormType implements ResolvedFormTypeInterface
|
||||
{
|
||||
private ?OptionsResolver $oDynamicOptionsResolver = null;
|
||||
|
||||
/**
|
||||
* Creates a new builder instance.
|
||||
*
|
||||
@@ -30,4 +33,13 @@ class ResolvedFormType extends SymfonyResolvedFormType implements ResolvedFormTy
|
||||
// Wrap the builder in a DynamicFormBuilder
|
||||
return new FormBuilder($builder);
|
||||
}
|
||||
|
||||
public function GetDynamicOptionsResolver(): OptionsResolver
|
||||
{
|
||||
if (!$this->oDynamicOptionsResolver) {
|
||||
$this->oDynamicOptionsResolver = new OptionsResolver();
|
||||
}
|
||||
|
||||
return $this->oDynamicOptionsResolver;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user