Skip to content

Commit 2ca652f

Browse files
authored
Fix form control lookup multiple initial value (#2278)
1 parent 9787bdf commit 2ca652f

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

demos/_unit-test/dropdown-html.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
$htmlValues = [
1818
$makeTestStringFx('d') => $makeTestStringFx('dTitle'),
1919
$makeTestStringFx('u') => $makeTestStringFx('uTitle'),
20+
$makeTestStringFx('v') => $makeTestStringFx('vTitle'),
2021
'[0 ]' => '[ ""]', // https://github.com/atk4/ui/pull/2274
2122
];
2223

@@ -105,8 +106,8 @@
105106
$form->entity->set(
106107
$k,
107108
str_contains($k, 'json')
108-
? [$makeTestStringFx('d')]
109-
: $makeTestStringFx('d')
109+
? [$makeTestStringFx('d'), $makeTestStringFx('v')]
110+
: $makeTestStringFx('d') . (str_contains($k, 'multi') ? ',' . $makeTestStringFx('v') : '')
110111
);
111112
}
112113

@@ -126,7 +127,7 @@
126127
$view->invokeInit();
127128
$view->text->addParagraph($app->encodeJson($form->entity->get()));
128129
$view->text->addParagraph('match init: ' . ($form->entity->get() === $initData));
129-
$view->text->addParagraph('match u add: ' . ($form->entity->get() === $makeExpectedDataFx(static fn ($k) => (str_contains($k, 'multi') ? $makeTestStringFx('d') . ',' : '') . $makeTestStringFx('u'))));
130+
$view->text->addParagraph('match u add: ' . ($form->entity->get() === $makeExpectedDataFx(static fn ($k) => (str_contains($k, 'multi') ? $makeTestStringFx('d') . ',' . $makeTestStringFx('v') . ',' : '') . $makeTestStringFx('u'))));
130131
$view->text->addParagraph('match empty: ' . ($form->entity->get() === $makeExpectedDataFx(static fn () => '')));
131132
$view->text->addParagraph('match u only: ' . ($form->entity->get() === $makeExpectedDataFx(static fn () => $makeTestStringFx('u'))));
132133
$view->text->addParagraph('match json-like add: ' . ($form->entity->get() === $makeExpectedDataFx(static fn ($k) => (str_contains($k, 'multi') ? $makeTestStringFx('u') . ',' : '') . '[0 ]')));

src/Form/Control/Dropdown.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function getInputValue(): ?string
109109
// dropdown input tag accepts CSV formatted list of IDs
110110
return $this->entityField !== null
111111
? ($this->multiple && $this->entityField->getField()->type === 'json' && is_array($this->entityField->get())
112-
? implode(', ', $this->entityField->get())
112+
? implode(',', $this->entityField->get())
113113
: $this->getApp()->uiPersistence->typecastAttributeSaveField($this->entityField->getField(), $this->entityField->get()))
114114
: parent::getInputValue();
115115
}

src/Form/Control/Lookup.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function getInputValue(): ?string
146146
// dropdown input tag accepts CSV formatted list of IDs
147147
return $this->entityField !== null
148148
? ($this->multiple && $this->entityField->getField()->type === 'json' && is_array($this->entityField->get())
149-
? implode(', ', $this->entityField->get())
149+
? implode(',', $this->entityField->get())
150150
: $this->getApp()->uiPersistence->typecastAttributeSaveField($this->entityField->getField(), $this->entityField->get()))
151151
: parent::getInputValue();
152152
}
@@ -385,10 +385,15 @@ protected function initDropdown($jsChain): void
385385

386386
foreach ($this->model->createIteratorBy(
387387
$idField,
388-
$this->multiple && $this->entityField->getField()->type === 'json' && is_array($this->entityField->get())
388+
$this->multiple
389389
? 'in'
390390
: '=',
391-
$this->entityField->get()
391+
$this->multiple && !($this->entityField->getField()->type === 'json' && is_array($this->entityField->get()))
392+
? array_map(
393+
fn ($v) => $this->model->getPersistence()->typecastLoadField($this->entityField->getField(), $v),
394+
explode(',', $this->model->getPersistence()->typecastSaveField($this->entityField->getField(), $this->entityField->get()))
395+
)
396+
: $this->entityField->get()
392397
) as $entity) {
393398
$settings['values'][] = array_merge($this->renderRow($entity), ['selected' => true]);
394399
}

0 commit comments

Comments
 (0)