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;
|
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,6 +24,11 @@ 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';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,10 +38,25 @@ class EditableCountryDropdownField extends EditableFormField
|
|||||||
{
|
{
|
||||||
$fields = parent::getCMSFields();
|
$fields = parent::getCMSFields();
|
||||||
|
|
||||||
$fields->replaceField(
|
$fields->removeByName('Default');
|
||||||
'Default',
|
$fields->addFieldToTab(
|
||||||
|
'Root.Main',
|
||||||
DropdownField::create('Default', _t(__CLASS__ . '.DEFAULT', 'Default value'))
|
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;
|
return $fields;
|
||||||
@ -44,6 +69,11 @@ 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
|
// Set default
|
||||||
if ($this->Default) {
|
if ($this->Default) {
|
||||||
$field->setValue($this->Default);
|
$field->setValue($this->Default);
|
||||||
|
@ -40,4 +40,23 @@ class EditableCountryDropdownFieldTest extends SapphireTest
|
|||||||
$field->Default = 'nz';
|
$field->Default = 'nz';
|
||||||
$this->assertEquals($field->getFormField()->Value(), '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