mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
NEW empty default value for Country Dropdown
This commit is contained in:
parent
4d89705fe6
commit
8870833318
@ -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())
|
||||
->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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user