mirror of
https://github.com/Combodo/iTop.git
synced 2026-03-02 23:54:12 +01:00
SDK Form demonstrator basic events
This commit is contained in:
@@ -10,20 +10,52 @@ use Dict;
|
||||
use Exception;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType;
|
||||
use Symfonycasts\DynamicForms\DependentField;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class AttCodeGroupByType extends AbstractType
|
||||
{
|
||||
public function getParent()
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
return SymfonyChoiceType::class; // TODO: Change the autogenerated stub
|
||||
$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');
|
||||
});
|
||||
|
||||
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
|
||||
\IssueLog::Info($event->getForm()->getName().' PRE_SUBMIT');
|
||||
});
|
||||
|
||||
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($options): void {
|
||||
\IssueLog::Info($event->getForm()->getName().' POST_SUBMIT');
|
||||
call_user_func($options['callback'], $event);
|
||||
});
|
||||
}
|
||||
|
||||
public static function BuildSubField(DependentField $oDependentField, string $sQuery, array $aFormOptions = []): void
|
||||
public function getParent()
|
||||
{
|
||||
return SymfonyChoiceType::class;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
parent::configureOptions($resolver);
|
||||
$resolver->setDefined('callback')
|
||||
->setAllowedTypes('callback', 'callable');
|
||||
}
|
||||
|
||||
public static function BuildSubField(FormInterface $oForm, string $sName, string $sQuery, array $aFormOptions = []): void
|
||||
{
|
||||
$aFormOptions['choices'] = self::GetGroupByOptions($sQuery);
|
||||
$aFormOptions['multiple'] = false;
|
||||
$oDependentField->add(AttCodeGroupByType::class, $aFormOptions);
|
||||
$oForm->add($sName, AttCodeGroupByType::class, $aFormOptions);
|
||||
}
|
||||
|
||||
protected static function GetGroupByOptions($sOql)
|
||||
|
||||
@@ -8,10 +8,30 @@ namespace Combodo\iTop\FormType\Orm;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType as SymfonyTextareaType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class QueryType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
parent::buildForm($builder, $options);
|
||||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
|
||||
\IssueLog::Info($event->getForm()->getName().' PRE_SET_DATA');
|
||||
});
|
||||
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
|
||||
\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) {
|
||||
\IssueLog::Info($event->getForm()->getName().' POST_SUBMIT');
|
||||
});
|
||||
}
|
||||
|
||||
public function getParent(): ?string
|
||||
{
|
||||
return SymfonyTextareaType::class;
|
||||
|
||||
@@ -8,31 +8,56 @@ namespace Combodo\iTop\FormType\Orm;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType;
|
||||
use Symfonycasts\DynamicForms\DependentField;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class ValuesFromAttcodeType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
|
||||
\IssueLog::Info($event->getForm()->getName().' PRE_SET_DATA');
|
||||
});
|
||||
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
|
||||
\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) {
|
||||
\IssueLog::Info($event->getForm()->getName().' POST_SUBMIT');
|
||||
});
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return SymfonyChoiceType::class;
|
||||
}
|
||||
|
||||
public static function BuildSubField(DependentField $oDependentField, string $sQuery, ?string $sGroupByAttCode, array $aFormOptions = []): void
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
parent::configureOptions($resolver);
|
||||
}
|
||||
|
||||
public static function BuildSubField(FormInterface $oForm, string $sName, string $sQuery, ?string $sGroupByAttCode, array $aFormOptions = []): void
|
||||
{
|
||||
if (is_null($sGroupByAttCode)) {
|
||||
$aFormOptions['choices'] = [];
|
||||
return;
|
||||
} else {
|
||||
$oModelReflection = new \ModelReflectionRuntime();
|
||||
$oQuery = $oModelReflection->GetQuery($sQuery);
|
||||
$sClass = $oQuery->GetClass();
|
||||
$oAttDef = \MetaModel::GetAttributeDef($sClass, $sGroupByAttCode);
|
||||
|
||||
//$aFormOptions['inherit_data'] = true;
|
||||
$aFormOptions['choices'] = array_flip($oAttDef->GetAllowedValues() ?? []);
|
||||
}
|
||||
$aFormOptions['multiple'] = true;
|
||||
$aFormOptions['required'] = false;
|
||||
|
||||
$oDependentField->add(ValuesFromAttcodeType::class, $aFormOptions);
|
||||
$oForm->add($sName, ValuesFromAttcodeType::class, $aFormOptions);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user