diff --git a/assets/controllers/elements/structural_entity_select_controller.js b/assets/controllers/elements/structural_entity_select_controller.js index e775af8a..60831602 100644 --- a/assets/controllers/elements/structural_entity_select_controller.js +++ b/assets/controllers/elements/structural_entity_select_controller.js @@ -43,7 +43,6 @@ export default class extends Controller { selectOnTab: true, maxOptions: null, create: allowAdd ? this.createItem.bind(this) : false, - createFilter: /\D/, //Must contain a non-digit character, otherwise they would be recognized as DB ID searchField: [ {field: "text", weight : 2}, @@ -72,7 +71,8 @@ export default class extends Controller { createItem(input, callback) { callback({ - value: input, + //$%$ is a special value prefix, that is used to identify items, that are not yet in the DB + value: '$%$' + input, text: input, not_in_db_yet: true, }); diff --git a/src/Form/Type/Helper/StructuralEntityChoiceHelper.php b/src/Form/Type/Helper/StructuralEntityChoiceHelper.php index 402270ce..69763e38 100644 --- a/src/Form/Type/Helper/StructuralEntityChoiceHelper.php +++ b/src/Form/Type/Helper/StructuralEntityChoiceHelper.php @@ -136,9 +136,10 @@ class StructuralEntityChoiceHelper if ($element->getID() === null) { if ($element instanceof AbstractStructuralDBElement) { //Must be the same as the separator in the choice_loader, otherwise this will not work! - return $element->getFullPath('->'); + return '$%$' . $element->getFullPath('->'); } - return $element->getName(); + // '$%$' is the indicator prefix for a new entity + return '$%$' . $element->getName(); } return $element->getID(); diff --git a/src/Form/Type/StructuralEntityType.php b/src/Form/Type/StructuralEntityType.php index 18368289..7eafccd9 100644 --- a/src/Form/Type/StructuralEntityType.php +++ b/src/Form/Type/StructuralEntityType.php @@ -51,11 +51,14 @@ class StructuralEntityType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options): void { $builder->addEventListener(FormEvents::PRE_SUBMIT, function (PreSubmitEvent $event) { - //When the data contains non-digit characters, we assume that the user entered a new element. + //When the data starts with "$%$", we assume that the user entered a new element. //In that case we add the new element to our choice_loader $data = $event->getData(); - if (null === $data || !is_string($data) || $data === "" || ctype_digit($data)) { + if (str_starts_with($data, '$%$')) { + //Extract the real name from the data + $data = substr($data, 3); + } else { return; }