mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
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:
parent
7195bd6cd6
commit
198befe2bc
3 changed files with 10 additions and 6 deletions
|
@ -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,
|
||||||
});
|
});
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue