Merge pull request #454 from micmania1/add-value-to-options

NEW added value to options
This commit is contained in:
Damian Mooyman 2016-07-04 18:18:02 +12:00 committed by GitHub
commit 446b98b3e8
4 changed files with 74 additions and 4 deletions

View File

@ -40,7 +40,13 @@ class EditableMultipleOptionField extends EditableFormField {
'callback' => function($record, $column, $grid) { 'callback' => function($record, $column, $grid) {
return TextField::create($column); return TextField::create($column);
} }
), ),
'Value' => array(
'title' => _t('EditableMultipleOptionField.VALUE', 'Value'),
'callback' => function($record, $column, $grid) {
return TextField::create($column);
}
),
'Default' => array( 'Default' => array(
'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'), 'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'),
'callback' => function($record, $column, $grid) { 'callback' => function($record, $column, $grid) {
@ -171,7 +177,7 @@ class EditableMultipleOptionField extends EditableFormField {
*/ */
protected function getOptionsMap() { protected function getOptionsMap() {
$optionSet = $this->Options(); $optionSet = $this->Options();
$optionMap = $optionSet->map('EscapedTitle', 'Title'); $optionMap = $optionSet->map('Value', 'Title');
if($optionMap instanceof SS_Map) { if($optionMap instanceof SS_Map) {
return $optionMap->toArray(); return $optionMap->toArray();
} }

View File

@ -15,7 +15,8 @@ class EditableOption extends DataObject {
"Name" => "Varchar(255)", "Name" => "Varchar(255)",
"Title" => "Varchar(255)", "Title" => "Varchar(255)",
"Default" => "Boolean", "Default" => "Boolean",
"Sort" => "Int" "Sort" => "Int",
"Value" => "Varchar(255)",
); );
private static $has_one = array( private static $has_one = array(
@ -31,6 +32,26 @@ class EditableOption extends DataObject {
'Default' 'Default'
); );
protected static $allow_empty_values = false;
/**
* Returns whether to allow empty values or not.
*
* @return boolean
*/
public static function allow_empty_values() {
return (bool) self::$allow_empty_values;
}
/**
* Set whether to allow empty values.
*
* @param boolean $allow
*/
public static function set_allow_empty_values($allow) {
self::$allow_empty_values = (bool) $allow;
}
/** /**
* @param Member $member * @param Member $member
* *
@ -114,4 +135,19 @@ class EditableOption extends DataObject {
public function canUnpublish($member = null) { public function canUnpublish($member = null) {
return $this->canDelete($member); return $this->canDelete($member);
} }
/**
* Fetches a value for $this->Value. If empty values are not allowed,
* then this will return the title in the case of an empty value.
*
* @return string
*/
public function getValue()
{
$value = $this->getField('Value');
if(empty($value) && !self::allow_empty_values()) {
return $this->Title;
}
return $value;
}
} }

View File

@ -70,6 +70,26 @@ class EditableFormFieldTest extends FunctionalTest {
$this->assertEquals(0, $checkbox->EffectiveDisplayRules()->count()); $this->assertEquals(0, $checkbox->EffectiveDisplayRules()->count());
} }
/**
* @covers EditableOption::getValue
*/
public function testEditableOptionEmptyValue() {
$option = $this->objFromFixture('EditableOption', 'option-1');
$option->Value = '';
// Disallow empty values
EditableOption::set_allow_empty_values(false);
$this->assertEquals($option->Title, $option->Value);
$option->Value = 'test';
$this->assertEquals('test', $option->Value);
// Allow empty values
EditableOption::set_allow_empty_values(true);
$option->Value = '';
$this->assertEquals('', $option->Value);
}
function testEditableDropdownField() { function testEditableDropdownField() {
$dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown'); $dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown');

View File

@ -8,34 +8,42 @@ EditableOption:
option-1: option-1:
Name: Option1 Name: Option1
Title: Option 1 Title: Option 1
Value: Option 1
option-2: option-2:
Name: Option2 Name: Option2
Title: Option 2 Title: Option 2
Value: Option 2
department-1: department-1:
Name: dept1 Name: dept1
Title: sales@example.com Title: sales@example.com
Value: sales@example.com
department-2: department-2:
Name: dept2 Name: dept2
Title: accounts@example.com Title: accounts@example.com
Value: accounts@example.com
option-3: option-3:
Name: Option3 Name: Option3
Title: Option 3 Title: Option 3
Value: Option 3
option-4: option-4:
Name: Option4 Name: Option4
Title: Option 4 Title: Option 4
Value: Option 4
option-5: option-5:
Name: Option5 Name: Option5
Title: Option 5 Title: Option 5
Value: Option 5
option-6: option-6:
Name: Option6 Name: Option6
Title: Option 6 Title: Option 6
Value: Option 6
UserDefinedForm_EmailRecipient: UserDefinedForm_EmailRecipient:
recipient-1: recipient-1:
@ -52,7 +60,7 @@ UserDefinedForm_EmailRecipient:
no-data: no-data:
EmailAddress: nodata@example.com EmailAddress: nodata@example.com
EmailSubject: Email Subject EmailSubject: Email Subject
EmailFrom: no-reply@example.com EmailFrom: no-reply@example.com
HideFormData: true HideFormData: true
EditableTextField: EditableTextField: