depage-forms
v2.0.0
html forms made easy
Toggle main menu visibility
Loading...
Searching...
No Matches
Multiple.php
Go to the documentation of this file.
1
<?php
2
10
11
namespace
Depage\HtmlForm\Elements;
12
13
use Depage\HtmlForm\Abstracts;
14
59
class
Multiple
extends
Abstracts\Input
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 $options =
null
, array|
string
|
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
();
189
$errorMessage
= $this->
htmlErrorMessage
();
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>"
202
.
$errorMessage
203
.
$helpMessage
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>"
210
.
$errorMessage
211
.
$helpMessage
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 : */
Depage\HtmlForm\Abstracts\Element\$name
$name
Element name.
Definition
Element.php:36
Depage\HtmlForm\Abstracts\Element\htmlEscape
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition
Element.php:263
Depage\HtmlForm\Abstracts\Input
input element base class
Definition
Input.php:22
Depage\HtmlForm\Abstracts\Input\$value
$value
Input elements's value.
Definition
Input.php:124
Depage\HtmlForm\Abstracts\Input\$label
$label
Input element - HTML label.
Definition
Input.php:37
Depage\HtmlForm\Abstracts\Input\$class
$class
class for paragraph
Definition
Input.php:152
Depage\HtmlForm\Abstracts\Input\$errorMessage
$errorMessage
Message that gets displayed in case of invalid input.
Definition
Input.php:218
Depage\HtmlForm\Abstracts\Input\htmlHelpMessage
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition
Input.php:685
Depage\HtmlForm\Abstracts\Input\htmlWrapperAttributes
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition
Input.php:636
Depage\HtmlForm\Abstracts\Input\htmlMarker
htmlMarker()
Returns elements' required-indicator.
Definition
Input.php:566
Depage\HtmlForm\Abstracts\Input\$helpMessage
$helpMessage
Extra help message.
Definition
Input.php:230
Depage\HtmlForm\Abstracts\Input\htmlValue
htmlValue()
Returns HTML-rendered element value.
Definition
Input.php:656
Depage\HtmlForm\Abstracts\Input\$marker
$marker
Input element - HTML marker text that marks required fields.
Definition
Input.php:92
Depage\HtmlForm\Abstracts\Input\htmlErrorMessage
htmlErrorMessage()
Returns HTML-rendered error message.
Definition
Input.php:666
Depage\HtmlForm\Elements\Multiple
HTML-multiple-choice input type i.e.
Definition
Multiple.php:60
Depage\HtmlForm\Elements\Multiple\htmlInputAttributes
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition
Multiple.php:224
Depage\HtmlForm\Elements\Multiple\$list
$list
Contains list of selectable options.
Definition
Multiple.php:64
Depage\HtmlForm\Elements\Multiple\$skin
$skin
HTML skin type (checkbox or select).
Definition
Multiple.php:78
Depage\HtmlForm\Elements\Multiple\typeCastValue
typeCastValue()
Converts value to element specific type.
Definition
Multiple.php:243
Depage\HtmlForm\Elements\Multiple\__construct
__construct(string $name, array $parameters, object $form)
multiple class constructor
Definition
Multiple.php:92
Depage\HtmlForm\Elements\Multiple\__toString
__toString()
Renders element to HTML.
Definition
Multiple.php:183
Depage\HtmlForm\Elements\Multiple\$maxItems
$maxItems
maxItems
Definition
Multiple.php:83
Depage\HtmlForm\Elements\Multiple\$listHtml
$listHtml
Contains list of html prerendered options.
Definition
Multiple.php:73
Depage\HtmlForm\Elements\Multiple\htmlList
htmlList(?array $options=null, array|string|null $value=null)
HTML option list rendering.
Definition
Multiple.php:133
Depage\HtmlForm\Elements\Multiple\setDefaults
setDefaults()
collects initial values across subclasses.
Definition
Multiple.php:110
Elements
Multiple.php
Generated on
for depage-forms by
doxygen
1.18.0
For an overview of all docs, go to
docs.depage.net