Allow to dynamically create elements with purely numeric names in a selector type

Before this was not possible, as this was messed up with the DB ids. Now we prefix the new created values with a special prefix, to mark them as new.

This fixes issue #381
This commit is contained in:
Jan Böhmer 2023-09-24 15:28:35 +02:00
parent 7195bd6cd6
commit 198befe2bc
3 changed files with 10 additions and 6 deletions

View file

@ -43,7 +43,6 @@ export default class extends Controller {
selectOnTab: true, selectOnTab: true,
maxOptions: null, maxOptions: null,
create: allowAdd ? this.createItem.bind(this) : false, create: allowAdd ? this.createItem.bind(this) : false,
createFilter: /\D/, //Must contain a non-digit character, otherwise they would be recognized as DB ID
searchField: [ searchField: [
{field: "text", weight : 2}, {field: "text", weight : 2},
@ -72,7 +71,8 @@ export default class extends Controller {
createItem(input, callback) { createItem(input, callback) {
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, text: input,
not_in_db_yet: true, not_in_db_yet: true,
}); });

View file

@ -136,9 +136,10 @@ class StructuralEntityChoiceHelper
if ($element->getID() === null) { if ($element->getID() === null) {
if ($element instanceof AbstractStructuralDBElement) { if ($element instanceof AbstractStructuralDBElement) {
//Must be the same as the separator in the choice_loader, otherwise this will not work! //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(); return $element->getID();

View file

@ -51,11 +51,14 @@ class StructuralEntityType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options): void
{ {
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (PreSubmitEvent $event) { $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 //In that case we add the new element to our choice_loader
$data = $event->getData(); $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; return;
} }