Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Forms/ListboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ public function getValueArray()
$replaced = [];
foreach ($value as $item) {
if (!is_array($item) && is_string($item)) {
// Schema listbox values can be posted as JSON strings with a Value key.
$trimmed = trim($item);
if ($trimmed !== '') {
$firstChar = substr($trimmed, 0, 1);
Expand Down
42 changes: 42 additions & 0 deletions tests/php/Forms/ListboxFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public static function provideSaveIntoValueScenarios(): array
'input' => '0',
'expected' => ['0'],
],
'schema-json-values' => [
'input' => [
'{"Value":"123","Title":"String integer"}',
'{"Value":"ABC123","Title":"Alpha-numeric string"}',
],
'expected' => ['123', 'ABC123'],
],
];
}

Expand All @@ -171,6 +178,41 @@ public function testSaveIntoPreservesValueTypes(mixed $input, array $expected):
$this->assertSame($expected, json_decode($obj->Choices, true));
}

public function testSaveIntoPreservesNumericStringFirstChoice(): void
{
$choices = [
'123' => 'Numeric string',
'ABC123' => 'Alpha-numeric string',
];
$field = new ListboxField('Choices', 'Choices', $choices);
$obj = new TestObject();
$field->setValue(['123', 'ABC123']);
$field->saveInto($obj);
$this->assertSame(['123', 'ABC123'], json_decode($obj->Choices, true));
}

public static function provideGetValueArrayMixedTypeScenarios(): array
{
return [
'numeric-source-mixed-values' => [
'source' => [
1 => 'One',
2 => 'Two',
],
'input' => ['1', 2, '2', 1],
'expected' => ['1', 2, '2', 1],
],
];
}

#[DataProvider('provideGetValueArrayMixedTypeScenarios')]
public function testGetValueArrayPreservesMixedTypes(array $source, array $input, array $expected): void
{
$field = new ListboxField('Choices', 'Choices', $source);
$field->setValue($input);
$this->assertSame($expected, $field->getValueArray());
}

public function testSaveIntoManyManyRelation()
{
$article = $this->objFromFixture(Article::class, 'articlewithouttags');
Expand Down
Loading