From 4d89705fe6fbb37274c4f222ecb01894022c831b Mon Sep 17 00:00:00 2001 From: Chen Shenghan Date: Sun, 29 Apr 2018 21:33:05 +1200 Subject: [PATCH 1/3] NEW default value for Country Dropdown --- .../EditableCountryDropdownField.php | 13 +++++++++++-- .../EditableCountryDropdownFieldTest.php | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/code/Model/EditableFormField/EditableCountryDropdownField.php b/code/Model/EditableFormField/EditableCountryDropdownField.php index dd38a74..af60f38 100644 --- a/code/Model/EditableFormField/EditableCountryDropdownField.php +++ b/code/Model/EditableFormField/EditableCountryDropdownField.php @@ -22,13 +22,17 @@ class EditableCountryDropdownField extends EditableFormField private static $table_name = 'EditableCountryDropdownField'; /** - * @return FieldList + * @return \SilverStripe\Forms\FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); - $fields->removeByName('Default'); + $fields->replaceField( + 'Default', + DropdownField::create('Default', _t(__CLASS__ . '.DEFAULT', 'Default value')) + ->setSource(i18n::getData()->getCountries()) + ); return $fields; } @@ -40,6 +44,11 @@ class EditableCountryDropdownField extends EditableFormField ->setFieldHolderTemplate(EditableFormField::class . '_holder') ->setTemplate(EditableDropdown::class); + // Set default + if ($this->Default) { + $field->setValue($this->Default); + } + $this->doUpdateFormField($field); return $field; diff --git a/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php b/tests/Model/EditableFormField/EditableCountryDropdownFieldTest.php index cfff955..4668253 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,22 @@ 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'); + } } From 887083331875b8b68f9d8cf5c9bef8fd42b277fe Mon Sep 17 00:00:00 2001 From: Chen Shenghan Date: Tue, 1 May 2018 16:07:03 +1200 Subject: [PATCH 2/3] NEW empty default value for Country Dropdown --- .../EditableCountryDropdownField.php | 36 +++++++++++++++++-- .../EditableCountryDropdownFieldTest.php | 19 ++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) 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); + } } From b205ca952a8ff29e5b0e1e8baffde3181bf8a155 Mon Sep 17 00:00:00 2001 From: Chen Shenghan Date: Wed, 2 May 2018 13:57:47 +1200 Subject: [PATCH 3/3] NEW default value for Country Dropdown (add i18n to the new fields) --- .../EditableFormField/EditableCountryDropdownField.php | 6 ++---- lang/en.yml | 3 +++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/code/Model/EditableFormField/EditableCountryDropdownField.php b/code/Model/EditableFormField/EditableCountryDropdownField.php index c37d78c..4683363 100644 --- a/code/Model/EditableFormField/EditableCountryDropdownField.php +++ b/code/Model/EditableFormField/EditableCountryDropdownField.php @@ -49,14 +49,12 @@ class EditableCountryDropdownField extends EditableFormField $fields->addFieldToTab( 'Root.Main', - CheckboxField::create('UseEmptyString') - ->setTitle('Set default empty string') + CheckboxField::create('UseEmptyString', _t(__CLASS__ . '.USE_EMPTY_STRING', 'Set default empty string')) ); $fields->addFieldToTab( 'Root.Main', - TextField::create('EmptyString') - ->setTitle('Empty String') + TextField::create('EmptyString', _t(__CLASS__ . '.EMPTY_STRING', 'Empty String')) ); return $fields; 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: