mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API: Change DropdownField::getSource() to not return the emptyString value.
This commit is contained in:
parent
1c983bc16d
commit
fee54c75f0
@ -149,29 +149,24 @@ class DropdownField extends FormField {
|
|||||||
public function Field($properties = array()) {
|
public function Field($properties = array()) {
|
||||||
$source = $this->getSource();
|
$source = $this->getSource();
|
||||||
$options = array();
|
$options = array();
|
||||||
if($source) {
|
|
||||||
// SQLMap needs this to add an empty value to the options
|
if ($this->getHasEmptyDefault()) {
|
||||||
if(is_object($source) && $this->emptyString) {
|
$selected = ($this->value === '' || $this->value === null);
|
||||||
|
$disabled = (in_array('', $this->disabledItems, true)) ? 'disabled' : false;
|
||||||
|
|
||||||
$options[] = new ArrayData(array(
|
$options[] = new ArrayData(array(
|
||||||
'Value' => '',
|
'Value' => '',
|
||||||
'Title' => $this->emptyString,
|
'Title' => $this->getEmptyString(),
|
||||||
|
'Selected' => $selected,
|
||||||
|
'Disabled' => $disabled
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($source) {
|
||||||
foreach($source as $value => $title) {
|
foreach($source as $value => $title) {
|
||||||
$selected = false;
|
|
||||||
if($value === '' && ($this->value === '' || $this->value === null)) {
|
|
||||||
$selected = true;
|
|
||||||
} else {
|
|
||||||
// check against value, fallback to a type check comparison when !value
|
// check against value, fallback to a type check comparison when !value
|
||||||
$selected = ($value) ? $value == $this->value : $value === $this->value;
|
$selected = ($value) ? ($value == $this->value) : ($value === $this->value);
|
||||||
$this->isSelected = $selected;
|
$disabled = (in_array($value, $this->disabledItems, true)) ? 'disabled' : false;
|
||||||
}
|
|
||||||
|
|
||||||
$disabled = false;
|
|
||||||
if(in_array($value, $this->disabledItems) && $title != $this->emptyString ){
|
|
||||||
$disabled = 'disabled';
|
|
||||||
}
|
|
||||||
|
|
||||||
$options[] = new ArrayData(array(
|
$options[] = new ArrayData(array(
|
||||||
'Title' => $title,
|
'Title' => $title,
|
||||||
@ -182,33 +177,42 @@ class DropdownField extends FormField {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$properties = array_merge($properties, array('Options' => new ArrayList($options)));
|
$properties = array_merge($properties, array(
|
||||||
|
'Options' => new ArrayList($options)
|
||||||
|
));
|
||||||
|
|
||||||
return parent::Field($properties);
|
return parent::Field($properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mark certain elements as disabled,
|
* Mark certain elements as disabled, regardless of the
|
||||||
* regardless of the {@link setDisabled()} settings.
|
* {@link setDisabled()} settings.
|
||||||
*
|
*
|
||||||
* @param array $items Collection of array keys, as defined in the $source array
|
* @param array $items Collection of array keys, as defined in the $source array
|
||||||
*/
|
*/
|
||||||
public function setDisabledItems($items) {
|
public function setDisabledItems($items) {
|
||||||
$this->disabledItems = $items;
|
$this->disabledItems = $items;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getDisabledItems() {
|
public function getDisabledItems() {
|
||||||
return $this->disabledItems;
|
return $this->disabledItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getAttributes() {
|
public function getAttributes() {
|
||||||
return array_merge(
|
return array_merge(
|
||||||
parent::getAttributes(),
|
parent::getAttributes(),
|
||||||
array('type' => null, 'value' => null)
|
array(
|
||||||
|
'type' => null,
|
||||||
|
'value' => null
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,18 +229,15 @@ class DropdownField extends FormField {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getSource() {
|
public function getSource() {
|
||||||
if(is_array($this->source) && $this->getHasEmptyDefault()) {
|
|
||||||
return array('' => $this->emptyString) + (array) $this->source;
|
|
||||||
} else {
|
|
||||||
return $this->source;
|
return $this->source;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $source
|
* @param array $source
|
||||||
*/
|
*/
|
||||||
public function setSource($source) {
|
public function setSource($source) {
|
||||||
$this->source = $source;
|
$this->source = $source;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,6 +246,7 @@ class DropdownField extends FormField {
|
|||||||
*/
|
*/
|
||||||
public function setHasEmptyDefault($bool) {
|
public function setHasEmptyDefault($bool) {
|
||||||
$this->hasEmptyDefault = $bool;
|
$this->hasEmptyDefault = $bool;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,14 +259,16 @@ class DropdownField extends FormField {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the default selection label, e.g. "select...".
|
* Set the default selection label, e.g. "select...".
|
||||||
* Defaults to an empty string. Automatically sets
|
*
|
||||||
* {@link $hasEmptyDefault} to true.
|
* Defaults to an empty string. Automatically sets {@link $hasEmptyDefault}
|
||||||
|
* to true.
|
||||||
*
|
*
|
||||||
* @param string $str
|
* @param string $str
|
||||||
*/
|
*/
|
||||||
public function setEmptyString($str) {
|
public function setEmptyString($str) {
|
||||||
$this->setHasEmptyDefault(true);
|
$this->setHasEmptyDefault(true);
|
||||||
$this->emptyString = $str;
|
$this->emptyString = $str;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,6 +279,9 @@ class DropdownField extends FormField {
|
|||||||
return $this->emptyString;
|
return $this->emptyString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return LookupField
|
||||||
|
*/
|
||||||
public function performReadonlyTransformation() {
|
public function performReadonlyTransformation() {
|
||||||
$field = $this->castedCopy('LookupField');
|
$field = $this->castedCopy('LookupField');
|
||||||
$field->setSource($this->getSource());
|
$field->setSource($this->getSource());
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<select $AttributesHTML>
|
<select $AttributesHTML>
|
||||||
<% loop $Options %>
|
<% loop $Options %>
|
||||||
<option value="$Value.XML"<% if $Selected %> selected="selected"<% end_if %><% if $Disabled %> disabled="disabled"<% end_if %>>$Title.XML</option>
|
<option value="$Value.XML"<% if $Selected %> selected="selected"<% end_if %><% if $Disabled %> disabled="disabled"<% end_if %>><% if Title %>$Title.XML<% else %> <% end_if %></option>
|
||||||
<% end_loop %>
|
<% end_loop %>
|
||||||
</select>
|
</select>
|
||||||
|
@ -25,45 +25,52 @@ class DropdownFieldTest extends SapphireTest {
|
|||||||
$this->assertEquals($matches[0], 'Yes');
|
$this->assertEquals($matches[0], 'Yes');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmptyStringAsLiteralConstructorArgument() {
|
|
||||||
$source = array(1 => 'one');
|
|
||||||
$field = new DropdownField('Field', null, $source);
|
|
||||||
$field->setEmptyString('select...');
|
|
||||||
$this->assertEquals(
|
|
||||||
$field->getSource(),
|
|
||||||
array(
|
|
||||||
'' => 'select...',
|
|
||||||
1 => 'one'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testHasEmptyDefault() {
|
public function testHasEmptyDefault() {
|
||||||
$source = array(1 => 'one');
|
$source = array(1 => 'one');
|
||||||
|
|
||||||
|
// Test getSource with empty
|
||||||
$field = new DropdownField('Field', null, $source);
|
$field = new DropdownField('Field', null, $source);
|
||||||
$field->setHasEmptyDefault(true);
|
$field->setHasEmptyDefault(true);
|
||||||
$this->assertEquals(
|
|
||||||
$field->getSource(),
|
|
||||||
array(
|
|
||||||
'' => '',
|
|
||||||
1 => 'one'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testEmptyDefaultStringThroughSetter() {
|
|
||||||
$source = array(1=>'one');
|
|
||||||
$field = new DropdownField('Field', null, $source);
|
|
||||||
$field->setEmptyString('select...');
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$field->getSource(),
|
$field->getSource(),
|
||||||
array(
|
array(
|
||||||
'' => 'select...',
|
|
||||||
1 => 'one'
|
1 => 'one'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertTrue(
|
|
||||||
$field->getHasEmptyDefault()
|
// Test that an empty option comes through in the markup however
|
||||||
|
$options = $this->findOptionElements($field->Field());
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
2,
|
||||||
|
count($options),
|
||||||
|
'Two options exist in the markup, one for the source, one for empty'
|
||||||
|
);
|
||||||
|
|
||||||
|
// the default value should be first
|
||||||
|
$first = array_shift($options);
|
||||||
|
$attrs = $first->attributes();
|
||||||
|
|
||||||
|
$this->assertNotEquals(
|
||||||
|
1,
|
||||||
|
$attrs['value'],
|
||||||
|
'First value is the not value (not the source value)'
|
||||||
|
);
|
||||||
|
|
||||||
|
// Test Field Without Empty
|
||||||
|
$FieldWithoutEmpty = new DropdownField('Field', null, $source);
|
||||||
|
$this->assertEquals(
|
||||||
|
$FieldWithoutEmpty->getSource(),
|
||||||
|
array(
|
||||||
|
1 => 'one'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
1,
|
||||||
|
count($options),
|
||||||
|
'As hasEmptyDefault is not provided, then no default option.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,10 +81,17 @@ class DropdownFieldTest extends SapphireTest {
|
|||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$field->getSource(),
|
$field->getSource(),
|
||||||
array(
|
array(
|
||||||
'' => 'select...',
|
|
||||||
0 => 'zero'
|
0 => 'zero'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$options = $this->findOptionElements($field->Field());
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
2,
|
||||||
|
count($options),
|
||||||
|
'Two options exist in the markup, one for the source, one for empty'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNumberOfSelectOptionsAvailable() {
|
public function testNumberOfSelectOptionsAvailable() {
|
||||||
@ -181,7 +195,10 @@ class DropdownFieldTest extends SapphireTest {
|
|||||||
);
|
);
|
||||||
|
|
||||||
$field = new DropdownField('Field', null, $source, $value);
|
$field = new DropdownField('Field', null, $source, $value);
|
||||||
if($emptyString !== null) $field->setEmptyString($emptyString);
|
|
||||||
|
if($emptyString !== null) {
|
||||||
|
$field->setEmptyString($emptyString);
|
||||||
|
}
|
||||||
|
|
||||||
return $field;
|
return $field;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user