11namespace Depage\HtmlForm\Elements;
13use Depage\HtmlForm\Abstracts;
87 parent::__construct(
$name, $parameters, $form);
89 $this->list = (isset($parameters[
'list']) && is_array($parameters[
'list'])) ? $parameters[
'list'] : [];
90 $this->listHtml = (isset($parameters[
'listHtml']) && is_array($parameters[
'listHtml'])) ? $parameters[
'listHtml'] : [];
104 parent::setDefaults();
107 $this->defaults[
'defaultValue'] =
'';
108 $this->defaults[
'skin'] =
'radio';
121 protected function htmlList(?array $options =
null, ?
string $value =
null): string
126 if ($options ==
null) {
133 if ($this->skin ===
"select") {
134 foreach ($options as $index => $option) {
135 if (is_array($option)) {
136 $list .=
"<optgroup label=\"{$index}\">" . $this->
htmlList($option,
$value) .
"</optgroup>";
138 $selected = ((string) $index === (string)
$value) ?
' selected' :
'';
139 $list .=
"<option value=\"{$index}\"{$selected}>{$option}</option>";
145 foreach ($options as $index => $option) {
147 $selected = ((string) $index === (string)
$value) ?
" checked=\"yes\"" :
'';
148 $class =
"input-single-option-" . str_replace(
" ",
"-", $index);
149 $optionHtml = $this->listHtml[$index] ??
"<span>{$option}</span>";
152 "<label class=\"{$class}\" title=\"{$option}\">" .
153 "<input type=\"radio\" name=\"{$this->name}\"{$inputAttributes} value=\"{$index}\"{$selected}>" .
171 $label = $this->htmlLabel();
177 if ($this->skin ===
"select") {
181 return "<p {$wrapperAttributes}>" .
183 "<span class=\"depage-label\">{$label}{$marker}</span>" .
184 "<select name=\"{$this->name}\"{$inputAttributes}>{$list}</select>" .
191 return "<p {$wrapperAttributes}>" .
192 "<span class=\"depage-label\">{$label}{$marker}</span>" .
193 "<span>{$list}</span>" .
210 if (in_array($this->value, array_keys($this->list))) {
214 foreach ($this->list as $sub) {
215 if (is_array($sub) && in_array($this->value, array_keys($sub))) {
224 $this->value = (string) $this->value;
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
htmlInputAttributes()
Returns string of HTML attributes for input element.
$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-single-choice input type i.e.
htmlList(?array $options=null, ?string $value=null)
Renders HTML - option list part of select/radio single element.
$list
Contains list of selectable options.
$skin
HTML skin type (radio or select).
typeCastValue()
Converts value to element specific type.
__construct(string $name, array $parameters, object $form)
single class constructor
__toString()
Renders element to HTML.
$listHtml
Contains list of html prerendered options.
setDefaults()
collects initial values across subclasses