1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* EditableDropdown |
5
|
|
|
* |
6
|
|
|
* Represents a modifiable dropdown (select) box on a form |
7
|
|
|
* |
8
|
|
|
* @property bool $UseEmptyString |
9
|
|
|
* @property string $EmptyString |
10
|
|
|
* |
11
|
|
|
* @package userforms |
12
|
|
|
*/ |
13
|
|
|
class EditableDropdown extends EditableMultipleOptionField |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
private static $singular_name = 'Dropdown Field'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
private static $plural_name = 'Dropdowns'; |
|
|
|
|
19
|
|
|
|
20
|
|
|
private static $db = array( |
|
|
|
|
21
|
|
|
'UseEmptyString' => 'Boolean', |
22
|
|
|
'EmptyString' => 'Varchar(255)', |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return FieldList |
27
|
|
|
*/ |
28
|
|
|
public function getCMSFields() |
29
|
|
|
{ |
30
|
|
|
$fields = parent::getCMSFields(); |
31
|
|
|
|
32
|
|
|
$fields->addFieldToTab( |
33
|
|
|
'Root.Main', |
34
|
|
|
CheckboxField::create('UseEmptyString') |
35
|
|
|
->setTitle('Set default empty string') |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$fields->addFieldToTab( |
39
|
|
|
'Root.Main', |
40
|
|
|
TextField::create('EmptyString') |
41
|
|
|
->setTitle('Empty String') |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$fields->removeByName('Default'); |
45
|
|
|
|
46
|
|
|
return $fields; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return ValidationResult |
51
|
|
|
*/ |
52
|
|
|
public function validate() |
53
|
|
|
{ |
54
|
|
|
$result = parent::validate(); |
55
|
|
|
|
56
|
|
|
if ($this->UseEmptyString && !$this->EmptyString) { |
57
|
|
|
$result->error('Please enter the default option text'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return DropdownField |
65
|
|
|
*/ |
66
|
|
|
public function getFormField() |
67
|
|
|
{ |
68
|
|
|
$field = DropdownField::create($this->Name, $this->EscapedTitle, $this->getOptionsMap()) |
|
|
|
|
69
|
|
|
->setFieldHolderTemplate('UserFormsField_holder') |
70
|
|
|
->setTemplate('UserFormsDropdownField'); |
71
|
|
|
|
72
|
|
|
if ($this->UseEmptyString) { |
73
|
|
|
$field->setEmptyString($this->EmptyString); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Set default |
77
|
|
|
$defaultOption = $this->getDefaultOptions()->first(); |
78
|
|
|
if ($defaultOption) { |
79
|
|
|
$field->setValue($defaultOption->EscapedTitle); |
80
|
|
|
} |
81
|
|
|
$this->doUpdateFormField($field); |
82
|
|
|
return $field; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
86
|
|
|
{ |
87
|
|
|
return "$(\"select[name='{$this->Name}']\")"; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.