From c8c9c2166d9839dbb4fff200175726f70add5ee6 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 15 Oct 2008 12:39:09 +0000 Subject: [PATCH] ENHANCEMENT Added DropdownField->setEmptyString() and DropdownField->setHasEmptyDefault() to decrease constructor arguments and allow for modification of behaviour after calling construtor. Added DropdownField->getSource() to dynamically include empty defaults at rendering-time rather than construction time. Adjusted DropdownField subclasses to new behaviour ENHANCEMENT Added DropdownFieldTest git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64304 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/CountryDropdownField.php | 7 ++- forms/DropdownField.php | 99 +++++++++++++++++++++++++++---- forms/GroupedDropdownField.php | 23 ++++--- forms/ListboxField.php | 10 ++-- forms/LookupField.php | 24 ++++---- forms/OptionsetField.php | 5 +- tests/forms/DropdownFieldTest.php | 85 ++++++++++++++++++++++++++ 7 files changed, 210 insertions(+), 43 deletions(-) create mode 100644 tests/forms/DropdownFieldTest.php diff --git a/forms/CountryDropdownField.php b/forms/CountryDropdownField.php index f435ed887..8195b6a34 100644 --- a/forms/CountryDropdownField.php +++ b/forms/CountryDropdownField.php @@ -8,11 +8,11 @@ class CountryDropdownField extends DropdownField { protected $defaultToVisitorCountry = true; - function __construct($name, $title, $source = null, $value = "", $form=null, $emptyString="--select--") { + function __construct($name, $title, $source = null, $value = "", $form=null) { if(!is_array($source)) { $source = Geoip::getCountryDropDown(); } - parent::__construct($name, $title, $source, $value, $form, $emptyString); + parent::__construct($name, $title, $source, $value, $form); } function defaultToVisitorCountry($val) { @@ -20,7 +20,8 @@ class CountryDropdownField extends DropdownField { } function Field() { - if($this->defaultToVisitorCountry && !$this->value || !isset($this->source[$this->value])) { + $source = $this->getSource(); + if($this->defaultToVisitorCountry && !$this->value || !isset($source[$this->value])) { $this->value = ($vc = Geoip::visitor_country()) ? $vc : Geoip::$default_country_code; } return parent::Field(); diff --git a/forms/DropdownField.php b/forms/DropdownField.php index 6193bd84f..d018c3c77 100755 --- a/forms/DropdownField.php +++ b/forms/DropdownField.php @@ -5,8 +5,39 @@ * @subpackage fields-basic */ class DropdownField extends FormField { + + /** + * @var boolean $source Associative or numeric array of all dropdown items, + * with array key as the submitted field value, and the array value as a + * natural language description shown in the interface element. + */ protected $source; - protected $isSelected, $disabled; + + /** + * @var boolean $isSelected Determines if the field was selected + * at the time it was rendered, so if {@link $value} matches on of the array + * values specified in {@link $source} + */ + protected $isSelected; + + /** + * @var boolean $disabled + */ + protected $disabled; + + /** + * @var boolean $hasEmptyDefault Show the first