Merge pull request #763 from zzdjk6/master

NEW default value for Country Dropdown

Allows editors to select their default country so users aren't required to scroll through a list of all countries, and also provides an empty default so validation isn't always a false pass (i.e. if a user neglects to select their correct country).
This commit is contained in:
Dylan Wagstaff 2018-05-03 10:15:03 +12:00 committed by GitHub
commit 8ab6b0506a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 1 deletions

View File

@ -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;

View File

@ -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:

View File

@ -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);
}
}