depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Multiple.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Elements;
12
13use Depage\HtmlForm\Abstracts;
14
60{
64 protected $list = [];
65
73 protected $listHtml = [];
74
78 protected $skin = 'radio';
79
83 protected $maxItems = null;
84
92 public function __construct(string $name, array $parameters, object $form)
93 {
94 parent::__construct($name, $parameters, $form);
95
96 $this->list = (isset($parameters['list']) && is_array($parameters['list'])) ? $parameters['list'] : [];
97 $this->listHtml = (isset($parameters['listHtml']) && is_array($parameters['listHtml'])) ? $parameters['listHtml'] : [];
98 $this->maxItems = isset($parameters['maxItems']) ? $parameters['maxItems'] : $this->maxItems;
99 }
100
110 protected function setDefaults(): void
111 {
112 parent::setDefaults();
113
114 // multiple-choice-elements have values of type array
115 $this->defaults['defaultValue'] = [];
116 $this->defaults['skin'] = 'checkbox';
117 $this->defaults['maxItems'] = null;
118 }
119
133 protected function htmlList(array|null $options = null, array|null $value = null): string
134 {
135 if ($value == null) {
136 $value = $this->htmlValue();
137 }
138 if ($options == null) {
139 $options = $this->list;
140 }
141
142 $options = $this->htmlEscape($options);
143 $list = '';
144
145 // select
146 if (in_array($this->skin, ['select', 'tags'])) {
147 foreach ($options as $index => $option) {
148 if (is_array($option)) {
149 $list .= "<optgroup label=\"{$index}\">" . $this->htmlList($option, $value) . "</optgroup>";
150 } else {
151 $selected = (in_array($index, $value)) ? ' selected' : '';
152 $list .= "<option value=\"{$index}\"{$selected}>{$option}</option>";
153 }
154 }
155 // checkbox
156 } else {
157 $inputAttributes = $this->htmlInputAttributes();
158
159 foreach ($options as $index => $option) {
160 $selected = (is_array($value) && (in_array($index, $value))) ? " checked=\"yes\"" : '';
161 $class = "input-multiple-option-" . str_replace(" ", "-", $index);
162 $optionHtml = $this->listHtml[$index] ?? "<span>{$option}</span>";
163
164 $list .= "<span>" .
165 "<label class=\"{$class}\" title=\"{$option}\">" .
166 "<input type=\"checkbox\" name=\"{$this->name}[]\"{$inputAttributes} value=\"{$index}\"{$selected}>" .
167 $optionHtml .
168 "</label>" .
169 "</span>";
170 }
171 }
172
173 return $list;
174 }
175
183 public function __toString(): string
184 {
185 $marker = $this->htmlMarker();
186 $label = $this->htmlLabel();
187 $list = $this->htmlList();
188 $wrapperAttributes = $this->htmlWrapperAttributes();
190 $helpMessage = $this->htmlHelpMessage();
191
192 if (in_array($this->skin, ['select', 'tags'])) {
193 // render HTML select
194
195 $inputAttributes = $this->htmlInputAttributes();
196
197 return "<p {$wrapperAttributes}>" .
198 "<label>" .
199 "<span class=\"depage-label\">{$label}{$marker}</span>" .
200 "<select multiple name=\"{$this->name}[]\"{$inputAttributes}>{$list}</select>" .
201 "</label>" .
204 "</p>\n";
205 } else {
206 // render HTML checkbox
207 return "<p {$wrapperAttributes}>" .
208 "<span class=\"depage-label\">{$label}{$marker}</span>" .
209 "<span>{$list}</span>" .
212 "</p>\n";
213 }
214 }
215
224 protected function htmlInputAttributes(): string
225 {
226 $attributes = '';
227
228 // HTML5 validator hack
229 if ($this->required && in_array($this->skin, ['select', 'tags'])) {
230 $attributes .= ' required="required"';
231 }
232 if ($this->maxItems) {
233 $attributes .= " data-max-items=\"$this->maxItems\"";
234 }
235 return $attributes;
236 }
237
243 protected function typeCastValue(): void
244 {
245 if ($this->value == "") {
246 $this->value = [];
247 } else {
248 $this->value = (array) $this->value;
249
250 if ($this->maxItems) {
251 $this->value = array_slice($this->value, 0, $this->maxItems);
252 }
253 }
254 }
255}
256
257/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition Element.php:261
input element base class
Definition Input.php:22
$value
Input elements's value.
Definition Input.php:124
$label
Input element - HTML label.
Definition Input.php:37
$class
class for paragraph
Definition Input.php:152
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:218
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:685
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:636
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:566
$helpMessage
Extra help message.
Definition Input.php:230
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:656
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:92
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:666
HTML-multiple-choice input type i.e.
Definition Multiple.php:60
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition Multiple.php:224
$list
Contains list of selectable options.
Definition Multiple.php:64
$skin
HTML skin type (checkbox or select).
Definition Multiple.php:78
typeCastValue()
Converts value to element specific type.
Definition Multiple.php:243
htmlList(array|null $options=null, array|null $value=null)
HTML option list rendering.
Definition Multiple.php:133
__construct(string $name, array $parameters, object $form)
multiple class constructor
Definition Multiple.php:92
__toString()
Renders element to HTML.
Definition Multiple.php:183
$listHtml
Contains list of html prerendered options.
Definition Multiple.php:73
setDefaults()
collects initial values across subclasses.
Definition Multiple.php:110