Skip to content

Commit

Permalink
Merge pull request #9894 from brettt89/fix/single-select-empty-default
Browse files Browse the repository at this point in the history
Move hasEmptySchema and emptyString to DataSchema on SingleSelectField
  • Loading branch information
emteknetnz authored Jan 17, 2022
2 parents 3db1435 + 600f8e5 commit 94f976d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Forms/SingleSelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ public function getSchemaStateDefaults()
{
$data = parent::getSchemaStateDefaults();

$data['value'] = $this->getDefaultValue();

return $data;
}

public function getSchemaDataDefaults()
{
$data = parent::getSchemaDataDefaults();

// Add options to 'data'
$data['data']['hasEmptyDefault'] = $this->getHasEmptyDefault();
$data['data']['emptyString'] = $this->getHasEmptyDefault() ? $this->getEmptyString() : null;

$data['value'] = $this->getDefaultValue();

return $data;
}

Expand Down
94 changes: 94 additions & 0 deletions tests/php/Forms/DropdownFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\FormTemplateHelper;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\View\ArrayData;
Expand Down Expand Up @@ -148,13 +149,106 @@ public function testHasEmptyDefault()
]
);

// Test that an empty option does not comes through in the markup however
$options = $this->findOptionElements($FieldWithoutEmpty->Field());

$this->assertEquals(
1,
count($options),
'As hasEmptyDefault is not provided, then no default option.'
);
}

public function testEmpty()
{
$fieldName = 'TestField';
$formName = 'testForm';
// Create mock form
$form = $this->createMock(Form::class);
$form->method('getTemplateHelper')
->willReturn(FormTemplateHelper::singleton());

$form->method('getHTMLID')
->willReturn($formName);

$source = [
'first' => 'value',
0 => 'otherValue'
];
$field = new DropdownField($fieldName, 'Test Field', $source);
$field->setForm($form);

$fieldId = $field->ID();
$this->assertEquals($fieldId, sprintf('%s_%s', $formName, $fieldName));

// Check state for default value
$schemaStateDefaults = $field->getSchemaStateDefaults();
$this->assertArraySubset(
[
'id' => $fieldId,
'name' => $fieldName,
'value' => 'first'
],
$schemaStateDefaults,
true
);

// Check data for empty defaults
$schemaDataDefaults = $field->getSchemaDataDefaults();
$this->assertArraySubset(
[
'id' => $fieldId,
'name' => $fieldName,
'type' => 'text',
'schemaType' => 'SingleSelect',
'holderId' => sprintf('%s_Holder', $fieldId),
'title' => 'Test Field',
'extraClass' => 'dropdown',
'data' => [
'emptyString' => null,
'hasEmptyDefault' => false
]
],
$schemaDataDefaults,
true
);

// Set an empty string of field
$field->setEmptyString('(Any)');

// Check state for default value
$schemaStateDefaults = $field->getSchemaStateDefaults();
$this->assertArraySubset(
[
'id' => $fieldId,
'name' => $fieldName,
'value' => ''
],
$schemaStateDefaults,
true
);

// Check data for empty defaults
$schemaDataDefaults = $field->getSchemaDataDefaults();
$this->assertArraySubset(
[
'id' => $fieldId,
'name' => $fieldName,
'type' => 'text',
'schemaType' => 'SingleSelect',
'holderId' => sprintf('%s_Holder', $fieldId),
'title' => 'Test Field',
'extraClass' => 'dropdown',
'data' => [
'emptyString' => '(Any)',
'hasEmptyDefault' => true
]
],
$schemaDataDefaults,
true
);
}

public function testZeroArraySourceNotOverwrittenByEmptyString()
{
$source = [0=>'zero'];
Expand Down

0 comments on commit 94f976d

Please sign in to comment.