11namespace Depage\HtmlForm\Elements;
13use Depage\HtmlForm\Abstracts;
94 parent::__construct(
$name, $parameters, $form);
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;
112 parent::setDefaults();
115 $this->defaults[
'defaultValue'] = [];
116 $this->defaults[
'skin'] =
'checkbox';
117 $this->defaults[
'maxItems'] =
null;
133 protected function htmlList(array|
null $options =
null, array|
null $value =
null): string
138 if ($options ==
null) {
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>";
151 $selected = (in_array($index,
$value)) ?
' selected' :
'';
152 $list .=
"<option value=\"{$index}\"{$selected}>{$option}</option>";
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>";
165 "<label class=\"{$class}\" title=\"{$option}\">" .
166 "<input type=\"checkbox\" name=\"{$this->name}[]\"{$inputAttributes} value=\"{$index}\"{$selected}>" .
186 $label = $this->htmlLabel();
192 if (in_array($this->skin, [
'select',
'tags'])) {
197 return "<p {$wrapperAttributes}>" .
199 "<span class=\"depage-label\">{$label}{$marker}</span>" .
200 "<select multiple name=\"{$this->name}[]\"{$inputAttributes}>{$list}</select>" .
207 return "<p {$wrapperAttributes}>" .
208 "<span class=\"depage-label\">{$label}{$marker}</span>" .
209 "<span>{$list}</span>" .
229 if ($this->required && in_array($this->skin, [
'select',
'tags'])) {
230 $attributes .=
' required="required"';
232 if ($this->maxItems) {
233 $attributes .=
" data-max-items=\"$this->maxItems\"";
245 if ($this->value ==
"") {
248 $this->value = (array) $this->value;
250 if ($this->maxItems) {
251 $this->value = array_slice($this->value, 0, $this->maxItems);
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
$value
Input elements's value.
$label
Input element - HTML label.
$class
class for paragraph
$errorMessage
Message that gets displayed in case of invalid input.
htmlHelpMessage()
Returns HTML-rendered helpMessage.
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
htmlMarker()
Returns elements' required-indicator.
$helpMessage
Extra help message.
htmlValue()
Returns HTML-rendered element value.
$marker
Input element - HTML marker text that marks required fields.
htmlErrorMessage()
Returns HTML-rendered error message.
HTML-multiple-choice input type i.e.
htmlInputAttributes()
Returns string of HTML attributes for input element.
$list
Contains list of selectable options.
$skin
HTML skin type (checkbox or select).
typeCastValue()
Converts value to element specific type.
htmlList(array|null $options=null, array|null $value=null)
HTML option list rendering.
__construct(string $name, array $parameters, object $form)
multiple class constructor
__toString()
Renders element to HTML.
$listHtml
Contains list of html prerendered options.
setDefaults()
collects initial values across subclasses.