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