SDK Form demonstrator refactor

This commit is contained in:
Eric Espie
2025-04-17 17:35:01 +02:00
parent a44b7b13f0
commit c522763412
2 changed files with 13 additions and 13 deletions

View File

@@ -16,10 +16,10 @@ abstract class AbstractType extends SymfonyAbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
if (isset($options['callback']) && isset($options['hook_type'])) {
$builder->addEventListener($options['hook_type'], function (FormEvent $event) use ($options): void {
\IssueLog::Info($event->getForm()->getName().' AbstractType.php'.$options['hook_type']);
call_user_func($options['callback'], $event);
if (isset($options['dynamic_form_hook.callable']) && isset($options['dynamic_form_hook.event_name'])) {
$builder->addEventListener($options['dynamic_form_hook.event_name'], function (FormEvent $event) use ($options): void {
\IssueLog::Info($event->getForm()->getName().' AbstractType.php'.$options['dynamic_form_hook.event_name']);
call_user_func($options['dynamic_form_hook.callable'], $event);
});
}
}
@@ -27,10 +27,10 @@ abstract class AbstractType extends SymfonyAbstractType
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefined('callback')
->setAllowedTypes('callback', 'callable');
$resolver->setDefined('hook_type')
->setAllowedTypes('hook_type', 'string');
$resolver->setDefined('dynamic_form_hook.callable')
->setAllowedTypes('dynamic_form_hook.callable', 'callable');
$resolver->setDefined('dynamic_form_hook.event_name')
->setAllowedTypes('dynamic_form_hook.event_name', 'string');
}
/**

View File

@@ -37,7 +37,7 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
});
}
private function GetCookedCallback(DependencyNode $oField, bool $IsHookedOnRootForm): callable
private function GetDynamicFormCallback(DependencyNode $oField, bool $IsHookedOnRootForm): callable
{
return function (FormEvent $event) use ($IsHookedOnRootForm, $oField) {
if ($IsHookedOnRootForm) {
@@ -55,8 +55,8 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
$aFieldOptions = $oType->BuildOptions($aUserOptions, $this->aModelData);
if (!is_null($aFieldOptions)) {
if ($oDependentField->HasChildren()) {
$aFieldOptions['callback'] = $this->GetCookedCallback($oDependentField, false);
$aFieldOptions['hook_type'] = ($event instanceof PostSetDataEvent) ? FormEvents::POST_SET_DATA : FormEvents::POST_SUBMIT;
$aFieldOptions['dynamic_form_hook.callable'] = $this->GetDynamicFormCallback($oDependentField, false);
$aFieldOptions['dynamic_form_hook.event_name'] = ($event instanceof PostSetDataEvent) ? FormEvents::POST_SET_DATA : FormEvents::POST_SUBMIT;
}
$oForm->add($oDependentField->GetName(), $sClass, $aFieldOptions);
} else {
@@ -81,8 +81,8 @@ class FormBuilder implements FormBuilderInterface, \IteratorAggregate
foreach ($this->oDependencies as $oField) {
if ($oField->HasChildren()) {
$this->addEventListener(FormEvents::POST_SET_DATA, $this->GetCookedCallback($oField, true));
$this->get($oField->GetName())->addEventListener(FormEvents::POST_SUBMIT, $this->GetCookedCallback($oField, false));
$this->addEventListener(FormEvents::POST_SET_DATA, $this->GetDynamicFormCallback($oField, true));
$this->get($oField->GetName())->addEventListener(FormEvents::POST_SUBMIT, $this->GetDynamicFormCallback($oField, false));
}
}
}