Fieldset.php
Go to the documentation of this file.
1<?php
11
13
51{
55 protected $form;
56
60 protected $label;
61
65 protected $disabled;
66
70 protected $required;
71
81 protected function setDefaults()
82 {
83 parent::setDefaults();
84
85 $this->defaults['label'] = $this->name;
86 $this->defaults['disabled'] = false;
87 $this->defaults['required'] = false;
88 }
89
96 public function setDisabled($disabled = true)
97 {
98 $this->disabled = (bool) $disabled;
99 }
100
107 public function getDisabled()
108 {
109 return $this->disabled;
110 }
111
126 public function addElement($type, $name, $parameters)
127 {
128 $this->form->checkElementName($name);
129
130 $newElement = parent::addElement($type, $name, $parameters);
131
132 if ( !($newElement instanceof fieldset) ) {
133 $this->form->updateInputValue($name);
134 }
135
136 return $newElement;
137 }
138
144 protected function htmlClasses()
145 {
146 $classes = '';
147
148 if ($this->required) {
149 $classes .= ' required';
150 }
151 if ($this->disabled) {
152 $classes .= ' disabled';
153 }
154 if (!empty($this->class)) {
155 $classes .= ' ' . $this->htmlEscape($this->class);
156 }
157
158 return trim($classes);
159 }
160
168 public function __toString()
169 {
170 $renderedElements = '';
171 $formName = $this->form->getName();
172 $label = $this->htmlLabel();
173 $classes = $this->htmlClasses();
174
175 $htmlAttributes = "id=\"{$formName}-{$this->name}\"";
176 $htmlAttributes .= " name=\"{$this->name}\"";
177 $htmlAttributes .= $this->htmlDataAttributes();
178
179 if (!empty($classes)) {
180 $htmlAttributes .= " class=\"" . $classes . "\"";
181 }
182
183
184 foreach ($this->elementsAndHtml as $element) {
185 $renderedElements .= $element;
186 }
187
188 return "<fieldset {$htmlAttributes}>" .
189 "<legend><span>{$label}</span></legend>{$renderedElements}" .
190 "</fieldset>\n";
191 }
192}
193
194/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
container element base class
Definition Container.php:22
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
Definition Element.php:290
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition Element.php:267
The fieldset class holds HTML-fieldset specific attributes and methods.
Definition Fieldset.php:51
addElement($type, $name, $parameters)
Generates sub-elements.
Definition Fieldset.php:126
setDisabled($disabled=true)
Sets the HTML disabled-attribute of the current input element.
Definition Fieldset.php:96
$required
HTML required attribute.
Definition Fieldset.php:70
$disabled
HTML disabled attribute.
Definition Fieldset.php:65
__toString()
Renders the fieldset to HTML code.
Definition Fieldset.php:168
htmlClasses()
Returns string of the elements' HTML-classes, separated by spaces.
Definition Fieldset.php:144
setDefaults()
collects initial values across subclasses.
Definition Fieldset.php:81
getDisabled()
Gets if input is currently disabled.
Definition Fieldset.php:107
Abstract element classes.
Definition Container.php:10
Classes for HTML input-elements.
Definition Address.php:9