Skip to content

Commit

Permalink
API CHANGE Use of the DropdownField $emptyString argument is now
Browse files Browse the repository at this point in the history
properly deprecated (has been marked as deprecated since 2.3). Please
use setEmptyString() on the DropdownField instance instead.
  • Loading branch information
halkyon committed May 23, 2012
1 parent 4267c03 commit 60c72c4
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 51 deletions.
30 changes: 18 additions & 12 deletions forms/DropdownField.php
Expand Up @@ -14,7 +14,6 @@
* public function getCMSFields() {
* $fields = parent::getCMSFields();
* $field = new DropdownField('GalleryID', 'Gallery', DataList::create('Gallery')->map('ID', 'Title'));
* $field->setHasEmptyDefault(true);
* $field->setEmptyString('(Select one)');
* $fields->addFieldToTab('Root.Content', $field, 'Content');
* </code>
Expand All @@ -29,10 +28,10 @@
* static $db = array(
* 'Country' => "Varchar(100)"
* );
* }
* }
* </code>
*
* Exampe instantiation:
* Example instantiation:
* <code>
* new DropdownField(
* 'Country',
Expand All @@ -55,7 +54,7 @@
* static $db = array(
* 'Country' => "Enum('New Zealand,United States,Germany','New Zealand')"
* );
* }
* }
* </code>
*
* Field construction:
Expand Down Expand Up @@ -113,14 +112,21 @@ class DropdownField extends FormField {
* @param $form The parent form
* @param $emptyString mixed Add an empty selection on to of the {@link $source}-Array
* (can also be boolean, which results in an empty string)
* Argument is deprecated in 2.3, please use {@link setHasEmptyDefault()} and {@link setEmptyString()} instead.
* Argument is deprecated in 3.1, please use {@link setEmptyString()} and/or {@link setHasEmptyDefault(true)} instead.
*/
function __construct($name, $title = null, $source = array(), $value = "", $form = null, $emptyString = null) {
function __construct($name, $title = null, $source = array(), $value = '', $form = null, $emptyString = null) {
$this->setSource($source);


if($emptyString === true) {
Deprecation::notice('3.1', 'Please use setHasEmptyDefault(true) instead of passing a boolean true $emptyString argument');
}
if(is_string($emptyString)) {
Deprecation::notice('3.1', 'Please use setEmptyString() instead of passing a string $emptyString argument.');
}

if($emptyString) $this->setHasEmptyDefault(true);
if(is_string($emptyString)) $this->setEmptyString($emptyString);

parent::__construct($name, ($title===null) ? $name : $title, $value, $form);
}

Expand Down Expand Up @@ -172,20 +178,20 @@ function getAttributes() {
function isSelected() {
return $this->isSelected;
}

/**
* Gets the source array including any empty default values.
*
* @return array
*/
function getSource() {
if(is_array($this->source) && $this->getHasEmptyDefault()) {
return array(""=>$this->emptyString) + (array)$this->source;
return array('' => $this->emptyString) + (array) $this->source;
} else {
return $this->source;
}
}

/**
* @param array $source
*/
Expand Down Expand Up @@ -213,7 +219,7 @@ function getHasEmptyDefault() {
* Set the default selection label, e.g. "select...".
* Defaults to an empty string. Automatically sets
* {@link $hasEmptyDefault} to true.
*
*
* @param string $str
*/
function setEmptyString($str) {
Expand Down
5 changes: 3 additions & 2 deletions model/fieldtypes/Boolean.php
Expand Up @@ -50,7 +50,8 @@ public function scaffoldSearchField($title = null) {
0 => _t('Boolean.NO', 'No')
);

return new DropdownField($this->name, $title, $source, '', null, "($anyText)");
$field = new DropdownField($this->name, $title, $source);
$field->setEmptyString("($anyText)");
}

/**
Expand All @@ -60,7 +61,7 @@ public function scaffoldSearchField($title = null) {
function prepValueForDB($value) {
if(strpos($value, '[')!==false)
return Convert::raw2sql($value);
else {
else {
if($value && strtolower($value) != 'f') {
return "'1'";
} else {
Expand Down
9 changes: 5 additions & 4 deletions model/fieldtypes/Enum.php
Expand Up @@ -65,15 +65,16 @@ function formField($title = null, $name = null, $hasEmpty = false, $value = "",
if(!$title) $title = $this->name;
if(!$name) $name = $this->name;

$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form, $emptyString);

return $field;
$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form);
$field->setEmptyString($emptyString);

return $field;
}

public function scaffoldFormField($title = null, $params = null) {
return $this->formField($title);
}

function scaffoldSearchField($title = null) {
$anyText = _t('Enum.ANY', 'Any');
return $this->formField($title, null, false, '', null, "($anyText)");
Expand Down
5 changes: 3 additions & 2 deletions model/fieldtypes/ForeignKey.php
Expand Up @@ -41,9 +41,10 @@ public function scaffoldFormField($title = null, $params = null) {
// Don't scaffold a dropdown for large tables, as making the list concrete
// might exceed the available PHP memory in creating too many DataObject instances
if($list->count() < 100) {
$field = new DropdownField($this->name, $title, $list->map("ID", $titleField), null, null, ' ');
$field = new DropdownField($this->name, $title, $list->map('ID', $titleField));
$field->setEmptyString(' ');
} else {
$field = new NumericField($this->name, $title);
$field = new NumericField($this->name, $title);
}

}
Expand Down
8 changes: 5 additions & 3 deletions model/fieldtypes/PrimaryKey.php
Expand Up @@ -25,9 +25,11 @@ function __construct($name, $object) {
}

public function scaffoldFormField($title = null, $params = null) {
$titleField = ($this->object->hasField('Title')) ? "Title" : "Name";
$map = DataList::create(get_class($this->object))->map("ID", $titleField);
return new DropdownField($this->name, $title, $map, null, null, ' ');
$titleField = ($this->object->hasField('Title')) ? 'Title' : 'Name';
$map = DataList::create(get_class($this->object))->map('ID', $titleField);
$field = new DropdownField($this->name, $title, $map);
$field->setEmptyString(' ');
return $field;
}
}

6 changes: 3 additions & 3 deletions security/Group.php
Expand Up @@ -123,8 +123,8 @@ public function getCMSFields() {
if(count($editorConfigMap) > 1) {
$fields->addFieldToTab('Root.Permissions',
new DropdownField(
'HtmlEditorConfig',
'HTML Editor Configuration',
'HtmlEditorConfig',
'HTML Editor Configuration',
$editorConfigMap
),
'Permissions'
Expand Down Expand Up @@ -479,4 +479,4 @@ public function requireDefaultRecords() {
// Members are populated through Member->requireDefaultRecords()
}

}
}
43 changes: 18 additions & 25 deletions tests/forms/DropdownFieldTest.php
Expand Up @@ -17,39 +17,29 @@ function testGetSource() {
}

function testReadonlyField() {
$dropdownField = new DropdownField('FeelingOk', 'Are you feeling ok?', array(0 => 'No', 1 => 'Yes'), '', null, '(Select one)');
$dropdownField->setValue(1);
$readonlyDropdownField = $dropdownField->performReadonlyTransformation();
preg_match('/Yes/', $dropdownField->Field(), $matches);
$field = new DropdownField('FeelingOk', 'Are you feeling ok?', array(0 => 'No', 1 => 'Yes'));
$field->setEmptyString('(Select one)');
$field->setValue(1);
$readonlyField = $field->performReadonlyTransformation();
preg_match('/Yes/', $field->Field(), $matches);
$this->assertEquals($matches[0], 'Yes');
}

function testEmptyStringAsBooleanConstructorArgument() {
$source = array(1=>'one');
$field = new DropdownField('Field', null, $source, null, null, true);
$this->assertEquals(
$field->getSource(),
array(
'' => '',
1 => 'one'
)
);
}


function testEmptyStringAsLiteralConstructorArgument() {
$source = array(1=>'one');
$field = new DropdownField('Field', null, $source, null, null, 'select...');
$source = array(1 => 'one');
$field = new DropdownField('Field', null, $source);
$field->setEmptyString('select...');
$this->assertEquals(
$field->getSource(),
array(
"" => 'select...',
'' => 'select...',
1 => 'one'
)
);
}

function testHasEmptyDefault() {
$source = array(1=>'one');
$source = array(1 => 'one');
$field = new DropdownField('Field', null, $source);
$field->setHasEmptyDefault(true);
$this->assertEquals(
Expand All @@ -60,7 +50,7 @@ function testHasEmptyDefault() {
)
);
}

function testEmptyDefaultStringThroughSetter() {
$source = array(1=>'one');
$field = new DropdownField('Field', null, $source);
Expand All @@ -76,7 +66,7 @@ function testEmptyDefaultStringThroughSetter() {
$field->getHasEmptyDefault()
);
}

function testZeroArraySourceNotOverwrittenByEmptyString() {
$source = array(0=>'zero');
$field = new DropdownField('Field', null, $source);
Expand Down Expand Up @@ -171,7 +161,10 @@ function testDropdownField($emptyString = null, $value = '') {
1 => 'Yes'
);

return new DropdownField('Field', null, $source, $value, null, $emptyString);
$field = new DropdownField('Field', null, $source, $value);
$field->setEmptyString($emptyString);

return $field;
}

/**
Expand Down

0 comments on commit 60c72c4

Please sign in to comment.