Merge pull request #9599 from martinheise/groupeddropdown-readonly

BUG Readonly version of GroupedDropdownField
This commit is contained in:
Guy Marriott 2020-07-21 09:21:52 -07:00 committed by GitHub
commit 5e7f0a5685
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Forms;
use SilverStripe\ORM\ArrayLib;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
@ -104,4 +105,14 @@ class GroupedDropdownField extends DropdownField
);
return $values;
}
/**
* @return SingleLookupField
*/
public function performReadonlyTransformation()
{
$field = parent::performReadonlyTransformation();
$field->setSource(ArrayLib::flatten($this->getSource()));
return $field;
}
}

View File

@ -129,4 +129,39 @@ class GroupedDropdownFieldTest extends SapphireTest
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()
);
}
}