SDK Form demonstrator basic events callbacks for dynamic fields

This commit is contained in:
Eric Espie
2025-04-16 11:08:51 +02:00
parent 1a1fb26425
commit 1be84ced61
3 changed files with 25 additions and 12 deletions

View File

@@ -22,11 +22,11 @@ class AttCodeGroupByType extends AbstractType
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
\IssueLog::Info($event->getForm()->getName().' PRE_SET_DATA');
call_user_func($options['callback'], $event);
});
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($options): void {
\IssueLog::Info($event->getForm()->getName().' POST_SET_DATA');
call_user_func($options['callback'], $event);
});
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
@@ -51,9 +51,11 @@ class AttCodeGroupByType extends AbstractType
->setAllowedTypes('callback', 'callable');
}
public static function BuildSubField(FormInterface $oForm, string $sName, string $sQuery, array $aFormOptions = []): void
public static function BuildSubField(FormInterface $oForm, string $sName, array $aData, array $aFormOptions = []): void
{
$aFormOptions['choices'] = self::GetGroupByOptions($sQuery);
\IssueLog::Info('AttCodeGroupByType BuildSubField data: '.var_export($aData, true));
$aFormOptions['choices'] = self::GetGroupByOptions($aData['query']);
$aFormOptions['multiple'] = false;
$oForm->add($sName, AttCodeGroupByType::class, $aFormOptions);
}

View File

@@ -17,17 +17,19 @@ class QueryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
\IssueLog::Info($event->getForm()->getName().' PRE_SET_DATA');
});
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($options): void {
\IssueLog::Info($event->getForm()->getName().' POST_SET_DATA');
});
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
\IssueLog::Info($event->getForm()->getName().' PRE_SUBMIT');
});
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($options): void {
\IssueLog::Info($event->getForm()->getName().' POST_SUBMIT');
});
}

View File

@@ -13,6 +13,7 @@ use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use utils;
class ValuesFromAttcodeType extends AbstractType
{
@@ -42,17 +43,25 @@ class ValuesFromAttcodeType extends AbstractType
parent::configureOptions($resolver);
}
public static function BuildSubField(FormInterface $oForm, string $sName, string $sQuery, ?string $sGroupByAttCode, array $aFormOptions = []): void
public static function BuildSubField(FormInterface $oForm, string $sName, array $aData, array $aFormOptions = []): void
{
if (is_null($sGroupByAttCode)) {
\IssueLog::Info("ValuesFromAttcodeType BuildSubField data: ".var_export($aData, true));
if (utils::IsNullOrEmptyString($aData['group_by'] ?? null)) {
return;
} else {
$oModelReflection = new \ModelReflectionRuntime();
$oQuery = $oModelReflection->GetQuery($sQuery);
$oQuery = $oModelReflection->GetQuery($aData['query']);
$sClass = $oQuery->GetClass();
$oAttDef = \MetaModel::GetAttributeDef($sClass, $sGroupByAttCode);
$sAttCode = $aData['group_by'];
if (\MetaModel::IsValidAttCode($sClass, $sAttCode)) {
$oAttDef = \MetaModel::GetAttributeDef($sClass, $sAttCode);
$aFormOptions['choices'] = array_flip($oAttDef->GetAllowedValues() ?? []);
$aFormOptions['choices'] = array_flip($oAttDef->GetAllowedValues() ?? []);
} else {
$aFormOptions['choices'] = [];
}
}
$aFormOptions['multiple'] = true;
$aFormOptions['required'] = false;