depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Fieldset.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Elements;
12
13use Depage\HtmlForm\Abstracts;
14
50
52{
56 protected $form;
57
61 protected $label;
62
66 protected $disabled;
67
71 protected $required;
72
82 protected function setDefaults(): void
83 {
84 parent::setDefaults();
85
86 $this->defaults['label'] = $this->name;
87 $this->defaults['disabled'] = false;
88 $this->defaults['required'] = false;
89 }
90
105 public function addElement(string $type, string $name, array $parameters): object
106 {
107 $this->form->checkElementName($name);
108
109 $newElement = parent::addElement($type, $name, $parameters);
110
111 if (!($newElement instanceof fieldset)) {
112 $this->form->updateInputValue($name);
113 }
114
115 return $newElement;
116 }
117
123 protected function htmlClasses(): string
124 {
125 $classes = '';
126
127 if ($this->required) {
128 $classes .= ' required';
129 }
130 if ($this->disabled) {
131 $classes .= ' disabled';
132 }
133 if (!empty($this->class)) {
134 $classes .= ' ' . $this->htmlEscape($this->class);
135 }
136
137 return trim($classes);
138 }
139
147 public function __toString(): string
148 {
149 $renderedElements = '';
150 $formName = $this->form->getName();
151 $label = $this->htmlLabel();
152 $classes = $this->htmlClasses();
153
154 $htmlAttributes = "id=\"{$formName}-{$this->name}\"";
155 $htmlAttributes .= " name=\"{$this->name}\"";
156 $htmlAttributes .= $this->htmlDataAttributes();
157
158 if (!empty($classes)) {
159 $htmlAttributes .= " class=\"" . $classes . "\"";
160 }
161
162
163 foreach ($this->elementsAndHtml as $element) {
164 $renderedElements .= $element;
165 }
166
167 return "<fieldset {$htmlAttributes}>" .
168 "<legend><span>{$label}</span></legend>{$renderedElements}" .
169 "</fieldset>\n";
170 }
171}
172
173/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
container element base class
Definition Container.php:60
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
Definition Element.php:288
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition Element.php:261
The fieldset class holds HTML-fieldset specific attributes and methods.
Definition Fieldset.php:52
$required
HTML required attribute.
Definition Fieldset.php:71
$disabled
HTML disabled attribute.
Definition Fieldset.php:66
__toString()
Renders the fieldset to HTML code.
Definition Fieldset.php:147
htmlClasses()
Returns string of the elements' HTML-classes, separated by spaces.
Definition Fieldset.php:123
setDefaults()
collects initial values across subclasses.
Definition Fieldset.php:82
addElement(string $type, string $name, array $parameters)
Generates sub-elements.
Definition Fieldset.php:105