This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
FormSelect.php
326 lines (282 loc) · 9.31 KB
/
FormSelect.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Form\View\Helper;
use Zend\Form\Element\Hidden;
use Zend\Form\ElementInterface;
use Zend\Form\Element\Select as SelectElement;
use Zend\Form\Exception;
use Zend\Stdlib\ArrayUtils;
class FormSelect extends AbstractHelper
{
/**
* Attributes valid for the current tag
*
* Will vary based on whether a select, option, or optgroup is being rendered
*
* @var array
*/
protected $validTagAttributes;
/**
* Attributes valid for select
*
* @var array
*/
protected $validSelectAttributes = [
'name' => true,
'autocomplete' => true,
'autofocus' => true,
'disabled' => true,
'form' => true,
'multiple' => true,
'required' => true,
'size' => true
];
/**
* Attributes valid for options
*
* @var array
*/
protected $validOptionAttributes = [
'disabled' => true,
'selected' => true,
'label' => true,
'value' => true,
];
/**
* Attributes valid for option groups
*
* @var array
*/
protected $validOptgroupAttributes = [
'disabled' => true,
'label' => true,
];
protected $translatableAttributes = [
'label' => true,
];
/**
* @var FormHidden|null
*/
protected $formHiddenHelper;
/**
* Invoke helper as functor
*
* Proxies to {@link render()}.
*
* @param ElementInterface|null $element
* @return string|FormSelect
*/
public function __invoke(ElementInterface $element = null)
{
if (!$element) {
return $this;
}
return $this->render($element);
}
/**
* Render a form <select> element from the provided $element
*
* @param ElementInterface $element
* @throws Exception\InvalidArgumentException
* @throws Exception\DomainException
* @return string
*/
public function render(ElementInterface $element)
{
if (!$element instanceof SelectElement) {
throw new Exception\InvalidArgumentException(sprintf(
'%s requires that the element is of type Zend\Form\Element\Select',
__METHOD__
));
}
$name = $element->getName();
if (empty($name) && $name !== 0) {
throw new Exception\DomainException(sprintf(
'%s requires that the element has an assigned name; none discovered',
__METHOD__
));
}
$options = $element->getValueOptions();
if (($emptyOption = $element->getEmptyOption()) !== null) {
$options = ['' => $emptyOption] + $options;
}
$attributes = $element->getAttributes();
$value = $this->validateMultiValue($element->getValue(), $attributes);
$attributes['name'] = $name;
if (array_key_exists('multiple', $attributes) && $attributes['multiple']) {
$attributes['name'] .= '[]';
}
$this->validTagAttributes = $this->validSelectAttributes;
$rendered = sprintf(
'<select %s>%s</select>',
$this->createAttributesString($attributes),
$this->renderOptions($options, $value)
);
// Render hidden element
$useHiddenElement = method_exists($element, 'useHiddenElement')
&& method_exists($element, 'getUnselectedValue')
&& $element->useHiddenElement();
if ($useHiddenElement) {
$rendered = $this->renderHiddenElement($element) . $rendered;
}
return $rendered;
}
/**
* Render an array of options
*
* Individual options should be of the form:
*
* <code>
* array(
* 'value' => 'value',
* 'label' => 'label',
* 'disabled' => $booleanFlag,
* 'selected' => $booleanFlag,
* )
* </code>
*
* @param array $options
* @param array $selectedOptions Option values that should be marked as selected
* @return string
*/
public function renderOptions(array $options, array $selectedOptions = [])
{
$template = '<option %s>%s</option>';
$optionStrings = [];
$escapeHtml = $this->getEscapeHtmlHelper();
foreach ($options as $key => $optionSpec) {
$value = '';
$label = '';
$selected = false;
$disabled = false;
if (is_scalar($optionSpec)) {
$optionSpec = [
'label' => $optionSpec,
'value' => $key
];
}
if (isset($optionSpec['options']) && is_array($optionSpec['options'])) {
$optionStrings[] = $this->renderOptgroup($optionSpec, $selectedOptions);
continue;
}
if (isset($optionSpec['value'])) {
$value = $optionSpec['value'];
}
if (isset($optionSpec['label'])) {
$label = $optionSpec['label'];
}
if (isset($optionSpec['selected'])) {
$selected = $optionSpec['selected'];
}
if (isset($optionSpec['disabled'])) {
$disabled = $optionSpec['disabled'];
}
if (ArrayUtils::inArray($value, $selectedOptions)) {
$selected = true;
}
if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate(
$label,
$this->getTranslatorTextDomain()
);
}
$attributes = compact('value', 'selected', 'disabled');
if (isset($optionSpec['attributes']) && is_array($optionSpec['attributes'])) {
$attributes = array_merge($attributes, $optionSpec['attributes']);
}
$this->validTagAttributes = $this->validOptionAttributes;
$optionStrings[] = sprintf(
$template,
$this->createAttributesString($attributes),
$escapeHtml($label)
);
}
return implode("\n", $optionStrings);
}
/**
* Render an optgroup
*
* See {@link renderOptions()} for the options specification. Basically,
* an optgroup is simply an option that has an additional "options" key
* with an array following the specification for renderOptions().
*
* @param array $optgroup
* @param array $selectedOptions
* @return string
*/
public function renderOptgroup(array $optgroup, array $selectedOptions = [])
{
$template = '<optgroup%s>%s</optgroup>';
$options = [];
if (isset($optgroup['options']) && is_array($optgroup['options'])) {
$options = $optgroup['options'];
unset($optgroup['options']);
}
$this->validTagAttributes = $this->validOptgroupAttributes;
$attributes = $this->createAttributesString($optgroup);
if (!empty($attributes)) {
$attributes = ' ' . $attributes;
}
return sprintf(
$template,
$attributes,
$this->renderOptions($options, $selectedOptions)
);
}
/**
* Ensure that the value is set appropriately
*
* If the element's value attribute is an array, but there is no multiple
* attribute, or that attribute does not evaluate to true, then we have
* a domain issue -- you cannot have multiple options selected unless the
* multiple attribute is present and enabled.
*
* @param mixed $value
* @param array $attributes
* @return array
* @throws Exception\DomainException
*/
protected function validateMultiValue($value, array $attributes)
{
if (null === $value) {
return [];
}
if (!is_array($value)) {
return (array) $value;
}
if (!isset($attributes['multiple']) || !$attributes['multiple']) {
throw new Exception\DomainException(sprintf(
'%s does not allow specifying multiple selected values when the element does not have a multiple attribute set to a boolean true',
__CLASS__
));
}
return $value;
}
protected function renderHiddenElement(ElementInterface $element)
{
$hiddenElement = new Hidden($element->getName());
$hiddenElement->setValue($element->getUnselectedValue());
return $this->getFormHiddenHelper()->__invoke($hiddenElement);
}
/**
* @return FormHidden
*/
protected function getFormHiddenHelper()
{
if (!$this->formHiddenHelper) {
if (method_exists($this->view, 'plugin')) {
$this->formHiddenHelper = $this->view->plugin('formhidden');
}
if (!$this->formHiddenHelper instanceof FormHidden) {
$this->formHiddenHelper = new FormHidden();
}
}
return $this->formHiddenHelper;
}
}