ENHANCEMENT setEmptyString option on EditableDropdown

closes #17
This commit is contained in:
Nic Horstmeier 2016-11-16 17:04:54 -06:00
parent 22c7fb4d07
commit 6e10acf6cd
2 changed files with 67 additions and 1 deletions

View File

@ -1,12 +1,15 @@
<?php
/**
* EditableDropdown
*
* Represents a modifiable dropdown (select) box on a form
*
* @property bool $UseEmptyString
* @property string $EmptyString
*
* @package userforms
*/
class EditableDropdown extends EditableMultipleOptionField
{
@ -14,6 +17,11 @@ class EditableDropdown extends EditableMultipleOptionField
private static $plural_name = 'Dropdowns';
private static $db = array(
'UseEmptyString' => 'Boolean',
'EmptyString' => 'Varchar(255)',
);
/**
* @return FieldList
*/
@ -21,6 +29,18 @@ class EditableDropdown extends EditableMultipleOptionField
{
$fields = parent::getCMSFields();
$fields->addFieldToTab(
'Root.Main',
CheckboxField::create('UseEmptyString')
->setTitle('Set default empty string')
);
$fields->addFieldToTab(
'Root.Main',
TextField::create('EmptyString')
->setTitle('Empty String')
);
$fields->removeByName('Default');
return $fields;
@ -35,6 +55,10 @@ class EditableDropdown extends EditableMultipleOptionField
->setFieldHolderTemplate('UserFormsField_holder')
->setTemplate('UserFormsDropdownField');
if ($this->UseEmptyString) {
$field->setEmptyString(($this->EmptyString) ? $this->EmptyString : '');
}
// Set default
$defaultOption = $this->getDefaultOptions()->first();
if ($defaultOption) {

View File

@ -0,0 +1,42 @@
<?php
/**
* Tests the {@see EditableDropdown} class
*/
class EditableDropdownTest extends SapphireTest
{
public static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml';
public function setUp()
{
parent::setUp();
}
/**
* Tests that the field sets the empty string if set
*/
public function testFormField()
{
if (!$dropdown = EditableDropdown::get()->filter('UseEmptyString', true)->first()) {
$dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown');
$dropdown->UseEmptyString = true;
$dropdown->EmptyString = 'My Default Empty String';
$dropdown->write();
}
$field = $dropdown->getFormField();
$this->assertEquals($field->getEmptyString(), 'My Default Empty String');
$alternateDropdown = $this->objFromFixture('EditableDropdown', 'department-dropdown');
$formField = $alternateDropdown->getFormField();
$this->assertFalse($formField->getHasEmptyDefault());
$alternateDropdown->UseEmptyString = true;
$alternateDropdown->write();
$this->assertEquals($formField->getEmptyString(), '');
}
}