Version: current

     1 <?php
     2 /**
     3  * Multi-line listbox field, created from a <select> tag.
     4  * @package forms
     5  * @subpackage fields-basic
     6  */
     7 class ListboxField extends DropdownField {
     8
     9     /**
    10      * The size of the field in rows.
    11      * @var int
    12      */
    13     protected $size;
    14
    15     /**
    16      * Should the user be able to select multiple
    17      * items on this dropdown field?
    18      *
    19      * @var boolean
    20      */
    21     protected $multiple = false;
    22
    23     /**
    24      * Creates a new dropdown field.
    25      *
    26      * @param string $name The field name
    27      * @param string $title The field title
    28      * @param array $source An map of the dropdown items
    29      * @param string|array $value You can pass an array of values or a single value like a drop down to be selected
    30      * @param int $size Optional size of the select element
    31      * @param form The parent form
    32      */
    33     function __construct($name, $title = '', $source = array(), $value = '', $size = null, $multiple = false, $form = null) {
    34         if($size) $this->size = $size;
    35         if($multiple) $this->multiple = $multiple;
    36         parent::__construct($name, $title, $source, $value, $form);
    37     }
    38
    39     /**
    40      * Returns a <select> tag containing all the appropriate <option> tags
    41      */
    42     function Field() {
    43         $size = '';
    44         $multiple = '';
    45
    46         if($this->size) $size = "size=\"$this->size\"";
    47
    48         if($this->multiple) {
    49             $multiple = "multiple=\"multiple\"";
    50             $this->name .= '[]';
    51         }
    52
    53         $options = "";
    54
    55         // We have an array of values
    56         if(is_array($this->value)){
    57             // Loop through and figure out which values were selected.
    58
    59             foreach($this->getSource() as $value => $title) {
    60                 // Loop through the array of values to find out if this value is selected.
    61                 $selected = "";
    62                 foreach($this->value as $v){
    63                     if($value == $v) {
    64                         $selected = " selected=\"selected\"";
    65                         break;
    66                     }
    67                 }
    68                 $options .= "<option$selected value=\"$value\">$title</option>\n";
    69             }
    70         }else{
    71             // Listbox was based a singlular value, so treat it like a dropdown.
    72             foreach($this->getSource() as $value => $title) {
    73                 $selected = $value == $this->value ? " selected=\"selected\"" : "";
    74                 $options .= "<option$selected value=\"$value\">$title</option>";
    75             }
    76         }
    77
    78         $id = $this->id();
    79         return "<select $size $multiple name=\"$this->name\" id=\"$id\">$options</select>";
    80     }
    81
    82     /**
    83      * Sets the size of this dropdown in rows.
    84      * @param int $size The height in rows (e.g. 3)
    85      */
    86     function setSize($size) {
    87         $this->size = $size;
    88     }
    89
    90     /**
    91      * Sets this field to have a muliple select attribute
    92      * @param boolean $bool
    93      */
    94     function setMultiple($bool) {
    95         $this->multiple = $bool;
    96     }
    97
    98 }
    99 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -