mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
NEW Added option to allow empty values
This commit is contained in:
parent
d614f9b382
commit
cb605d5633
@ -32,6 +32,26 @@ class EditableOption extends DataObject {
|
||||
'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
|
||||
*
|
||||
@ -115,4 +135,19 @@ class EditableOption extends DataObject {
|
||||
public function canUnpublish($member = null) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +70,26 @@ class EditableFormFieldTest extends FunctionalTest {
|
||||
$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() {
|
||||
$dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user