diff --git a/code/Model/EditableFormField/EditableCountryDropdownField.php b/code/Model/EditableFormField/EditableCountryDropdownField.php index dd38a74..4683363 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,16 +24,38 @@ class EditableCountryDropdownField extends EditableFormField private static $plural_name = 'Country Dropdowns'; + private static $db = array( + 'UseEmptyString' => 'Boolean', + 'EmptyString' => 'Varchar(255)', + ); + private static $table_name = 'EditableCountryDropdownField'; /** - * @return FieldList + * @return \SilverStripe\Forms\FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName('Default'); + $fields->addFieldToTab( + 'Root.Main', + DropdownField::create('Default', _t(__CLASS__ . '.DEFAULT', 'Default value')) + ->setSource(i18n::getData()->getCountries()) + ->setHasEmptyDefault(true) + ->setEmptyString('---') + ); + + $fields->addFieldToTab( + 'Root.Main', + CheckboxField::create('UseEmptyString', _t(__CLASS__ . '.USE_EMPTY_STRING', 'Set default empty string')) + ); + + $fields->addFieldToTab( + 'Root.Main', + TextField::create('EmptyString', _t(__CLASS__ . '.EMPTY_STRING', 'Empty String')) + ); return $fields; } @@ -40,6 +67,16 @@ 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); + } + $this->doUpdateFormField($field); return $field; diff --git a/lang/en.yml b/lang/en.yml index b71a6f5..1fc9461 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -89,11 +89,14 @@ en: other: '{count} Checkbox Groups' SINGULARNAME: 'Checkbox Group' SilverStripe\UserForms\Model\EditableFormField\EditableCountryDropdownField: + DEFAULT: 'Default value' + EMPTY_STRING: 'Empty String' PLURALNAME: 'Country Dropdowns' PLURALS: one: 'A Country Dropdown' other: '{count} Country Dropdowns' SINGULARNAME: 'Country Dropdown' + USE_EMPTY_STRING: 'Set default empty string' SilverStripe\UserForms\Model\EditableFormField\EditableDateField: PLURALNAME: 'Date Fields' PLURALS: diff --git a/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php b/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php index cfff955..4bbd019 100644 --- a/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php +++ b/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php @@ -3,6 +3,7 @@ namespace SilverStripe\UserForms\Tests\Model\EditableFormField; use SilverStripe\Dev\SapphireTest; +use SilverStripe\Forms\DropdownField; use SilverStripe\UserForms\Model\EditableFormField\EditableCountryDropdownField; class EditableCountryDropdownFieldTest extends SapphireTest @@ -21,4 +22,41 @@ class EditableCountryDropdownFieldTest extends SapphireTest $field->Name = 'EditableFormField_123456'; $this->assertEmpty($field->getFormField()->Title()); } + + public function testCMSFieldsContainsDefaultValue() + { + /** @var EditableCountryDropdownField $field */ + $field = EditableCountryDropdownField::create(); + $cmsFields = $field->getCMSFields(); + $defaultField = $cmsFields->dataFieldByName('Default'); + $this->assertNotNull($defaultField); + $this->assertInstanceOf(DropdownField::class, $defaultField); + } + + public function testDefaultValue() + { + /** @var EditableCountryDropdownField $field */ + $field = EditableCountryDropdownField::create(); + $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); + } }