mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX: DropdownField didn't consider disabled items
This commit is contained in:
parent
dfb9b76b95
commit
68d8df4e04
@ -314,7 +314,9 @@ class DropdownField extends FormField {
|
||||
*/
|
||||
public function validate($validator) {
|
||||
$source = $this->getSourceAsArray();
|
||||
if (!array_key_exists($this->value, $source)) {
|
||||
$disabled = $this->getDisabledItems();
|
||||
|
||||
if (!array_key_exists($this->value, $source) || in_array($this->value, $disabled)) {
|
||||
if ($this->getHasEmptyDefault() && !$this->value) {
|
||||
return true;
|
||||
}
|
||||
|
@ -97,13 +97,17 @@ class GroupedDropdownField extends DropdownField {
|
||||
public function validate($validator) {
|
||||
$valid = false;
|
||||
$source = $this->getSourceAsArray();
|
||||
$disabled = $this->getDisabledItems();
|
||||
|
||||
if ($this->value) {
|
||||
foreach ($source as $value => $title) {
|
||||
// Check if value matches an option, or is in a nested array of grouped options
|
||||
if ((is_array($title) && array_key_exists($this->value, $title))
|
||||
|| ($this->value == $value)
|
||||
) {
|
||||
if (is_array($title) && array_key_exists($this->value, $title)) {
|
||||
// Check that the set value is not in the list of disabled items
|
||||
if (!isset($disabled[$value]) || !in_array($this->value, $disabled[$value])) {
|
||||
$valid = true;
|
||||
}
|
||||
// Check that the value matches and is not disabled
|
||||
} elseif($this->value == $value && !in_array($this->value, $disabled)) {
|
||||
$valid = true;
|
||||
}
|
||||
}
|
||||
|
@ -313,7 +313,8 @@ class DropdownFieldTest extends SapphireTest {
|
||||
public function testValidation() {
|
||||
$field = DropdownField::create('Test', 'Testing', array(
|
||||
"One" => "One",
|
||||
"Two" => "Two"
|
||||
"Two" => "Two",
|
||||
"Five" => "Five"
|
||||
));
|
||||
$validator = new RequiredFields();
|
||||
$form = new Form($this, 'Form', new FieldList($field), new FieldList(), $validator);
|
||||
@ -331,6 +332,10 @@ class DropdownFieldTest extends SapphireTest {
|
||||
$field->setEmptyString('Empty String');
|
||||
$field->setValue('');
|
||||
$this->assertTrue($field->validate($validator));
|
||||
//disabled items shouldn't validate
|
||||
$field->setDisabledItems(array('Five'));
|
||||
$field->setValue('Five');
|
||||
$this->assertFalse($field->validate($validator));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -39,6 +39,16 @@ class GroupedDropdownFieldTest extends SapphireTest {
|
||||
$field->setEmptyString('Empty String');
|
||||
$field->setValue('');
|
||||
$this->assertTrue($field->validate($validator));
|
||||
|
||||
//disabled items shouldn't validate
|
||||
$field->setDisabledItems(array('1'));
|
||||
$field->setValue('1');
|
||||
$this->assertFalse($field->validate($validator));
|
||||
|
||||
//grouped disabled items shouldn't validate
|
||||
$field->setDisabledItems(array("Group One" => array("2")));
|
||||
$field->setValue('2');
|
||||
$this->assertFalse($field->validate($validator));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user