Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

API CHANGE Use of the DropdownField $emptyString argument is now

properly deprecated (has been marked as deprecated since 2.3). Please
use setEmptyString() on the DropdownField instance instead.
  • Loading branch information...
commit 60c72c40d9b57460b8dfd14973e05a28de862581 1 parent 4267c03
@halkyon halkyon authored
View
30 forms/DropdownField.php
@@ -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>
@@ -29,10 +28,10 @@
* static $db = array(
* 'Country' => "Varchar(100)"
* );
- * }
+ * }
* </code>
*
- * Exampe instantiation:
+ * Example instantiation:
* <code>
* new DropdownField(
* 'Country',
@@ -55,7 +54,7 @@
* static $db = array(
* 'Country' => "Enum('New Zealand,United States,Germany','New Zealand')"
* );
- * }
+ * }
* </code>
*
* Field construction:
@@ -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);
}
@@ -172,7 +178,7 @@ function getAttributes() {
function isSelected() {
return $this->isSelected;
}
-
+
/**
* Gets the source array including any empty default values.
*
@@ -180,12 +186,12 @@ function isSelected() {
*/
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
*/
@@ -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) {
View
5 model/fieldtypes/Boolean.php
@@ -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)");
}
/**
@@ -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 {
View
9 model/fieldtypes/Enum.php
@@ -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)");
View
5 model/fieldtypes/ForeignKey.php
@@ -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);
}
}
View
8 model/fieldtypes/PrimaryKey.php
@@ -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;
}
}
View
6 security/Group.php
@@ -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'
@@ -479,4 +479,4 @@ public function requireDefaultRecords() {
// Members are populated through Member->requireDefaultRecords()
}
-}
+}
View
43 tests/forms/DropdownFieldTest.php
@@ -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(
@@ -60,7 +50,7 @@ function testHasEmptyDefault() {
)
);
}
-
+
function testEmptyDefaultStringThroughSetter() {
$source = array(1=>'one');
$field = new DropdownField('Field', null, $source);
@@ -76,7 +66,7 @@ function testEmptyDefaultStringThroughSetter() {
$field->getHasEmptyDefault()
);
}
-
+
function testZeroArraySourceNotOverwrittenByEmptyString() {
$source = array(0=>'zero');
$field = new DropdownField('Field', null, $source);
@@ -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;
}
/**

0 comments on commit 60c72c4

Please sign in to comment.
Something went wrong with that request. Please try again.