/
SingleSelectField.php
184 lines (160 loc) · 4.68 KB
/
SingleSelectField.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace SilverStripe\Forms;
use ArrayAccess;
/**
* Represents the base class for a single-select field
*/
abstract class SingleSelectField extends SelectField
{
/**
* Show the first <option> element as empty (not having a value),
* with an optional label defined through {@link $emptyString}.
* By default, the <select> element will be rendered with the
* first option from {@link $source} selected.
*
* @var bool
*/
protected $hasEmptyDefault = false;
/**
* The title shown for an empty default selection,
* e.g. "Select...".
*
* @var string
*/
protected $emptyString = '';
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_SINGLESELECT;
public function getSchemaStateDefaults()
{
$data = parent::getSchemaStateDefaults();
$data['value'] = $this->getDefaultValue();
return $data;
}
public function getSchemaDataDefaults()
{
$data = parent::getSchemaDataDefaults();
// Add options to 'data'
$data['data']['hasEmptyDefault'] = $this->getHasEmptyDefault();
$data['data']['emptyString'] = $this->getHasEmptyDefault() ? $this->getEmptyString() : null;
return $data;
}
public function getDefaultValue()
{
$value = $this->Value();
// assign value to field, such as first option available
if ($value === null) {
if ($this->getHasEmptyDefault()) {
$value = '';
} else {
$values = $this->getValidValues();
$value = array_shift($values);
}
}
return $value;
}
/**
* @param boolean $bool
* @return self Self reference
*/
public function setHasEmptyDefault($bool)
{
$this->hasEmptyDefault = $bool;
return $this;
}
/**
* @return bool
*/
public function getHasEmptyDefault()
{
return $this->hasEmptyDefault;
}
/**
* Set the default selection label, e.g. "select...".
* Defaults to an empty string. Automatically sets
* {@link $hasEmptyDefault} to true.
*
* @param string $string
* @return $this
*/
public function setEmptyString($string)
{
$this->setHasEmptyDefault(true);
$this->emptyString = $string;
return $this;
}
/**
* @return string
*/
public function getEmptyString()
{
return $this->emptyString;
}
/**
* Gets the source array, including the empty string, if present
*
* @return array|ArrayAccess
*/
public function getSourceEmpty()
{
// Inject default option
if ($this->getHasEmptyDefault()) {
return ['' => $this->getEmptyString()] + $this->getSource();
} else {
return $this->getSource();
}
}
/**
* Validate this field
*
* @param Validator $validator
* @return bool
*/
public function validate($validator)
{
// Check if valid value is given
$selected = $this->Value();
$validValues = $this->getValidValues();
if (strlen($selected ?? '')) {
// Use selection rules to check which are valid
foreach ($validValues as $formValue) {
if ($this->isSelectedValue($formValue, $selected)) {
return $this->extendValidationResult(true, $validator);
}
}
} else {
if ($this->getHasEmptyDefault() || !$validValues || in_array('', $validValues ?? [])) {
// Check empty value
return $this->extendValidationResult(true, $validator);
}
$selected = '(none)';
}
// Fail
$validator->validationError(
$this->name,
_t(
'SilverStripe\\Forms\\DropdownField.SOURCE_VALIDATION',
"Please select a value within the list provided. {value} is not a valid option",
['value' => $selected]
),
"validation"
);
return $this->extendValidationResult(false, $validator);
}
public function castedCopy($classOrCopy)
{
$field = parent::castedCopy($classOrCopy);
if ($field instanceof SingleSelectField && $this->getHasEmptyDefault()) {
$field->setEmptyString($this->getEmptyString());
}
return $field;
}
/**
* @return SingleLookupField
*/
public function performReadonlyTransformation()
{
$field = $this->castedCopy(SingleLookupField::class);
$field->setSource($this->getSource());
$field->setReadonly(true);
return $field;
}
}