mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
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:
commit
8ab6b0506a
@ -3,7 +3,9 @@
|
|||||||
namespace SilverStripe\UserForms\Model\EditableFormField;
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
||||||
|
|
||||||
use SilverStripe\Core\Manifest\ModuleLoader;
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
||||||
|
use SilverStripe\Forms\CheckboxField;
|
||||||
use SilverStripe\Forms\DropdownField;
|
use SilverStripe\Forms\DropdownField;
|
||||||
|
use SilverStripe\Forms\TextField;
|
||||||
use SilverStripe\i18n\i18n;
|
use SilverStripe\i18n\i18n;
|
||||||
use SilverStripe\UserForms\Model\EditableCustomRule;
|
use SilverStripe\UserForms\Model\EditableCustomRule;
|
||||||
use SilverStripe\UserForms\Model\EditableFormField;
|
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
|
* A dropdown field which allows the user to select a country
|
||||||
*
|
*
|
||||||
|
* @property bool $UseEmptyString
|
||||||
|
* @property string $EmptyString
|
||||||
|
*
|
||||||
* @package userforms
|
* @package userforms
|
||||||
*/
|
*/
|
||||||
class EditableCountryDropdownField extends EditableFormField
|
class EditableCountryDropdownField extends EditableFormField
|
||||||
@ -19,16 +24,38 @@ class EditableCountryDropdownField extends EditableFormField
|
|||||||
|
|
||||||
private static $plural_name = 'Country Dropdowns';
|
private static $plural_name = 'Country Dropdowns';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
|
'UseEmptyString' => 'Boolean',
|
||||||
|
'EmptyString' => 'Varchar(255)',
|
||||||
|
);
|
||||||
|
|
||||||
private static $table_name = 'EditableCountryDropdownField';
|
private static $table_name = 'EditableCountryDropdownField';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return FieldList
|
* @return \SilverStripe\Forms\FieldList
|
||||||
*/
|
*/
|
||||||
public function getCMSFields()
|
public function getCMSFields()
|
||||||
{
|
{
|
||||||
$fields = parent::getCMSFields();
|
$fields = parent::getCMSFields();
|
||||||
|
|
||||||
$fields->removeByName('Default');
|
$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;
|
return $fields;
|
||||||
}
|
}
|
||||||
@ -40,6 +67,16 @@ class EditableCountryDropdownField extends EditableFormField
|
|||||||
->setFieldHolderTemplate(EditableFormField::class . '_holder')
|
->setFieldHolderTemplate(EditableFormField::class . '_holder')
|
||||||
->setTemplate(EditableDropdown::class);
|
->setTemplate(EditableDropdown::class);
|
||||||
|
|
||||||
|
// Empty string
|
||||||
|
if ($this->UseEmptyString) {
|
||||||
|
$field->setEmptyString($this->EmptyString ?: '');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set default
|
||||||
|
if ($this->Default) {
|
||||||
|
$field->setValue($this->Default);
|
||||||
|
}
|
||||||
|
|
||||||
$this->doUpdateFormField($field);
|
$this->doUpdateFormField($field);
|
||||||
|
|
||||||
return $field;
|
return $field;
|
||||||
|
@ -89,11 +89,14 @@ en:
|
|||||||
other: '{count} Checkbox Groups'
|
other: '{count} Checkbox Groups'
|
||||||
SINGULARNAME: 'Checkbox Group'
|
SINGULARNAME: 'Checkbox Group'
|
||||||
SilverStripe\UserForms\Model\EditableFormField\EditableCountryDropdownField:
|
SilverStripe\UserForms\Model\EditableFormField\EditableCountryDropdownField:
|
||||||
|
DEFAULT: 'Default value'
|
||||||
|
EMPTY_STRING: 'Empty String'
|
||||||
PLURALNAME: 'Country Dropdowns'
|
PLURALNAME: 'Country Dropdowns'
|
||||||
PLURALS:
|
PLURALS:
|
||||||
one: 'A Country Dropdown'
|
one: 'A Country Dropdown'
|
||||||
other: '{count} Country Dropdowns'
|
other: '{count} Country Dropdowns'
|
||||||
SINGULARNAME: 'Country Dropdown'
|
SINGULARNAME: 'Country Dropdown'
|
||||||
|
USE_EMPTY_STRING: 'Set default empty string'
|
||||||
SilverStripe\UserForms\Model\EditableFormField\EditableDateField:
|
SilverStripe\UserForms\Model\EditableFormField\EditableDateField:
|
||||||
PLURALNAME: 'Date Fields'
|
PLURALNAME: 'Date Fields'
|
||||||
PLURALS:
|
PLURALS:
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace SilverStripe\UserForms\Tests\Model\EditableFormField;
|
namespace SilverStripe\UserForms\Tests\Model\EditableFormField;
|
||||||
|
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Forms\DropdownField;
|
||||||
use SilverStripe\UserForms\Model\EditableFormField\EditableCountryDropdownField;
|
use SilverStripe\UserForms\Model\EditableFormField\EditableCountryDropdownField;
|
||||||
|
|
||||||
class EditableCountryDropdownFieldTest extends SapphireTest
|
class EditableCountryDropdownFieldTest extends SapphireTest
|
||||||
@ -21,4 +22,41 @@ class EditableCountryDropdownFieldTest extends SapphireTest
|
|||||||
$field->Name = 'EditableFormField_123456';
|
$field->Name = 'EditableFormField_123456';
|
||||||
$this->assertEmpty($field->getFormField()->Title());
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user