Usage
*
*
* new OptionsetField(
* $name = "Foobar",
* $title = "FooBar's optionset",
* $source = array(
* "1" => "Option 1",
* "2" => "Option 2",
* "3" => "Option 3",
* "4" => "Option 4",
* "5" => "Option 5"
* ),
* $value = "1"
* );
*
*
* You can use the helper functions on data object set to create the source array. eg:
*
*
* //Database request for the object
* $myDoSet = DataObject::get("FooBars","");
* if($myDoSet){
* // This returns an array of ID => Title
* $map = $myDoSet->toDropDownMap();
*
* // Instantiate the OptionsetField
* $fieldset = new Fieldset(
* new OptionsetField(
* $name = "Foobar",
* $title = "FooBar's optionset",
* $source = $map,
* $value = $map[0]
* )
* );
* }
*
* // Pass the fields to the form constructor. etc
*
*
* @package forms
* @subpackage fields-basic
*/
class OptionsetField extends DropdownField {
/**
* @var Array
*/
protected $disabledItems = array();
/**
* Creates a new optionset field.
* @param name The field name
* @param title The field title
* @param source An map of the dropdown items
* @param value The current value
* @param form The parent form
*/
function __construct($name, $title = "", $source = array(), $value = "", $form = null) {
parent::__construct($name, $title, $source, $value, $form);
}
/**
* Create a UL tag containing sets of radio buttons and labels. The IDs are set to
* FieldID_ItemKey, where ItemKey is the key with all non-alphanumerics removed.
*
* @todo Should use CheckboxField FieldHolder rather than constructing own markup.
*/
function Field() {
$options = '';
$odd = 0;
$source = $this->getSource();
foreach($source as $key => $value) {
$itemID = $this->id() . "_" . ereg_replace('[^a-zA-Z0-9]+','',$key);
if($key == $this->value/* || $useValue */) {
$useValue = false;
$checked = " checked=\"checked\"";
} else {
$checked="";
}
$odd = ($odd + 1) % 2;
$extraClass = $odd ? "odd" : "even";
$extraClass .= " val" . preg_replace('/[^a-zA-Z0-9\-\_]/','_', $key);
$disabled = ($this->disabled || in_array($key, $this->disabledItems)) ? 'disabled="disabled"' : '';
$options .= "