-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTMLGroupedDropdownField.php
60 lines (49 loc) · 1.62 KB
/
HTMLGroupedDropdownField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
class HTML5GroupedDropdownField extends DropdownField {
static $extensions = array(
'HTML5FieldsDecorator'
);
// HTML tag used to render this field
protected $tagType = 'select';
// If this field is required, the HTML5 required attribute will be set
protected $requried = false;
// Regular expression validation
protected $pattern;
// Set multiple select attribute
protected $multiple = false;
/**
* @see HTML5FieldsDecorator::defaultField()
*/
public function Field() {
// Initialisations
$options = $classAttr = '';
$extraAttributes = array();
if($extraClass = trim($this->extraClass())) {
$classAttr = "class=\"$extraClass\"";
}
foreach($this->getSource() as $value => $title) {
if(is_array($title)) {
$options .= "<optgroup label=\"$value\">";
foreach($title as $value2 => $title2) {
$selected = $value2 == $this->value ? " selected=\"selected\"" : "";
$options .= "<option$selected value=\"$value2\">$title2</option>";
}
$options .= "</optgroup>";
} else { // Fall back to the standard dropdown field
$selected = $value == $this->value ? " selected=\"selected\"" : "";
$options .= "<option$selected value=\"$value\">$title</option>";
}
}
$id = $this->id();
if( $this->required ) $extraAttributes[] = 'required';
if( $this->multiple ) $extraAttributes[] = 'multiple';
return "<select $classAttr name=\"$this->name\" id=\"$id\"" . implode(' ', $extraAttributes) . ">$options</select>";
}
/**
* Sets this field to have a muliple select attribute
* @param boolean $bool
*/
public function setMultiple($bool) {
$this->multiple = $bool;
}
}