-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTML5DropdownField.php
73 lines (63 loc) · 1.77 KB
/
HTML5DropdownField.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
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
class HTML5DropdownField 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;
/**
* @see HTML5FieldsDecorator::defaultField()
*/
public function Field() {
$options = '';
if( $source = $this->getSource() ) {
// For SQLMap sources, the empty string needs to be added specially
if( is_object($source) && $this->emptyString ) {
$options .= $this->createTag('option', array('value' => ''), $this->emptyString);
}
foreach( $source as $value => $title ) {
// Blank value of field and source (e.g. "" => "(Any)")
if( $value === '' && ($this->value === '' || $this->value === null) ) {
$selected = 'selected';
} else {
// Normal value from the source
if( $value ) {
$selected = $value == $this->value ? 'selected' : null;
} else {
// Do a type check comparison, we might have an array key of 0
$selected = $value === $this->value ? 'selected' : null;
}
$this->isSelected = (bool) $selected;
}
$options .= $this->createTag(
'option',
array(
'selected' => $selected,
'value' => $value
),
Convert::raw2xml($title)
);
}
}
return $this->defaultField($options);
}
/**
* @see FormField::FieldHolder()
*/
public function FieldHolder() {
return $this->defaultFieldHolder();
}
/**
* Set the pattern attribute for in-browser regular expression validation
* Regular expressions have an implied ^(:? and )$
*
* @param string $regex
*/
public function setPattern( $regex ) {
$this->pattern = $regex;
}
}