diff --git a/code/Model/EditableFormField/EditableCountryDropdownField.php b/code/Model/EditableFormField/EditableCountryDropdownField.php index af60f38..c37d78c 100644 --- a/code/Model/EditableFormField/EditableCountryDropdownField.php +++ b/code/Model/EditableFormField/EditableCountryDropdownField.php @@ -3,7 +3,9 @@ namespace SilverStripe\UserForms\Model\EditableFormField; use SilverStripe\Core\Manifest\ModuleLoader; +use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\DropdownField; +use SilverStripe\Forms\TextField; use SilverStripe\i18n\i18n; use SilverStripe\UserForms\Model\EditableCustomRule; use SilverStripe\UserForms\Model\EditableFormField; @@ -11,6 +13,9 @@ use SilverStripe\UserForms\Model\EditableFormField; /** * A dropdown field which allows the user to select a country * + * @property bool $UseEmptyString + * @property string $EmptyString + * * @package userforms */ class EditableCountryDropdownField extends EditableFormField @@ -19,6 +24,11 @@ class EditableCountryDropdownField extends EditableFormField private static $plural_name = 'Country Dropdowns'; + private static $db = array( + 'UseEmptyString' => 'Boolean', + 'EmptyString' => 'Varchar(255)', + ); + private static $table_name = 'EditableCountryDropdownField'; /** @@ -28,10 +38,25 @@ class EditableCountryDropdownField extends EditableFormField { $fields = parent::getCMSFields(); - $fields->replaceField( - 'Default', + $fields->removeByName('Default'); + $fields->addFieldToTab( + 'Root.Main', DropdownField::create('Default', _t(__CLASS__ . '.DEFAULT', 'Default value')) - ->setSource(i18n::getData()->getCountries()) + ->setSource(i18n::getData()->getCountries()) + ->setHasEmptyDefault(true) + ->setEmptyString('---') + ); + + $fields->addFieldToTab( + 'Root.Main', + CheckboxField::create('UseEmptyString') + ->setTitle('Set default empty string') + ); + + $fields->addFieldToTab( + 'Root.Main', + TextField::create('EmptyString') + ->setTitle('Empty String') ); return $fields; @@ -44,6 +69,11 @@ class EditableCountryDropdownField extends EditableFormField ->setFieldHolderTemplate(EditableFormField::class . '_holder') ->setTemplate(EditableDropdown::class); + // Empty string + if ($this->UseEmptyString) { + $field->setEmptyString($this->EmptyString ?: ''); + } + // Set default if ($this->Default) { $field->setValue($this->Default); diff --git a/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php b/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php index 4668253..4bbd019 100644 --- a/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php +++ b/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php @@ -40,4 +40,23 @@ class EditableCountryDropdownFieldTest extends SapphireTest $field->Default = 'nz'; $this->assertEquals($field->getFormField()->Value(), 'nz'); } + + public function testEmptyDefaultValue() + { + /** @var EditableCountryDropdownField $field */ + $field = EditableCountryDropdownField::create(); + + /** @var DropdownField $formField */ + $formField = $field->getFormField(); + $this->assertFalse($formField->getHasEmptyDefault()); + $this->assertEmpty($formField->getEmptyString()); + + $field->UseEmptyString = true; + $field->EmptyString = '--- empty ---'; + + /** @var DropdownField $formField */ + $formField = $field->getFormField(); + $this->assertTrue($formField->getHasEmptyDefault()); + $this->assertEquals($formField->getEmptyString(), $field->EmptyString); + } }