Merge pull request #486 from halkyon/deprecate_dropdownfield_emptystring_arg

Deprecate DropdownField $emptyString argument properly
This commit is contained in:
Sean Harvey 2012-05-23 14:59:07 -07:00
commit 08bb9c63d0
7 changed files with 55 additions and 51 deletions

View File

@ -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>
@ -32,7 +31,7 @@
* }
* </code>
*
* Exampe instantiation:
* Example instantiation:
* <code>
* new DropdownField(
* 'Country',
@ -113,11 +112,18 @@ 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);
@ -180,7 +186,7 @@ class DropdownField extends FormField {
*/
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;
}

View File

@ -50,7 +50,8 @@ class Boolean extends DBField {
0 => _t('Boolean.NO', 'No')
);
return new DropdownField($this->name, $title, $source, '', null, "($anyText)");
$field = new DropdownField($this->name, $title, $source);
$field->setEmptyString("($anyText)");
}
/**

View File

@ -65,7 +65,8 @@ class Enum extends DBField {
if(!$title) $title = $this->name;
if(!$name) $name = $this->name;
$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form, $emptyString);
$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form);
$field->setEmptyString($emptyString);
return $field;
}

View File

@ -41,7 +41,8 @@ class ForeignKey extends Int {
// 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);
}

View File

@ -25,9 +25,11 @@ class PrimaryKey extends Int {
}
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 File

@ -17,39 +17,29 @@ class DropdownFieldTest extends SapphireTest {
}
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(
@ -171,7 +161,10 @@ class DropdownFieldTest extends SapphireTest {
1 => 'Yes'
);
return new DropdownField('Field', null, $source, $value, null, $emptyString);
$field = new DropdownField('Field', null, $source, $value);
$field->setEmptyString($emptyString);
return $field;
}
/**