Skip to content

Commit

Permalink
BUG Readonly version of GroupedDropdownField
Browse files Browse the repository at this point in the history
GroupedDropdownField was showing empty values in Readonly mode due to not correctly handling the hierarchical source array.
Uses flattened source now in GroupedDropdownField->performReadonlyTransformation()
  • Loading branch information
martinheise committed Jul 21, 2020
1 parent e341e0a commit 404f450
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Forms/GroupedDropdownField.php
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Forms;

use SilverStripe\ORM\ArrayLib;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;

Expand Down Expand Up @@ -104,4 +105,14 @@ function ($title, $value) use (&$values) {
);
return $values;
}

/**
* @return SingleLookupField
*/
public function performReadonlyTransformation()
{
$field = parent::performReadonlyTransformation();
$field->setSource(ArrayLib::flatten($this->getSource()));
return $field;
}
}
35 changes: 35 additions & 0 deletions tests/php/Forms/GroupedDropdownFieldTest.php
Expand Up @@ -129,4 +129,39 @@ public function testEmptyString()
preg_replace('/\s+/', ' ', (string)$field->Field())
);
}

/**
* Test that readonly version of GroupedDropdownField displays all values
*/
public function testReadonlyValue()
{
$field = GroupedDropdownField::create(
'Test',
'Testing',
[
"1" => "One",
"Group One" => [
"2" => "Two",
"3" => "Three"
],
"Group Two" => [
"4" => "Four"
],
]
);

// value on first level
$field->setValue("1");
$this->assertRegExp(
'/<span class="readonly" id="Test">One<\/span><input type="hidden" name="Test" value="1" \/>/',
(string)$field->performReadonlyTransformation()->Field()
);

// value on first level
$field->setValue("2");
$this->assertRegExp(
'/<span class="readonly" id="Test">Two<\/span><input type="hidden" name="Test" value="2" \/>/',
(string)$field->performReadonlyTransformation()->Field()
);
}
}

0 comments on commit 404f450

Please sign in to comment.