fieldset.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\elements;
11 
12 use depage\htmlform\abstracts;
13 
51 {
55  protected $form;
56 
66  protected function setDefaults()
67  {
68  parent::setDefaults();
69 
70  $this->defaults['label'] = $this->name;
71  $this->defaults['disabled'] = false;
72  $this->defaults['required'] = false;
73  }
74 
89  public function addElement($type, $name, $parameters)
90  {
91  $this->form->checkElementName($name);
92 
93  $newElement = parent::addElement($type, $name, $parameters);
94 
95  if ( !($newElement instanceof fieldset) ) {
96  $this->form->updateInputValue($name);
97  }
98 
99  return $newElement;
100  }
101 
107  protected function htmlClasses()
108  {
109  $classes = '';
110 
111  if ($this->required) {
112  $classes .= ' required';
113  }
114  if ($this->disabled) {
115  $classes .= ' disabled';
116  }
117  if (!empty($this->class)) {
118  $classes .= ' ' . $this->htmlEscape($this->class);
119  }
120 
121  return trim($classes);
122  }
123 
131  public function __toString()
132  {
133  $renderedElements = '';
134  $formName = $this->form->getName();
135  $label = $this->htmlLabel();
136  $classes = $this->htmlClasses();
137 
138  $htmlAttributes = "id=\"{$formName}-{$this->name}\"";
139  $htmlAttributes .= " name=\"{$this->name}\"";
140 
141  if (!empty($classes)) {
142  $htmlAttributes .= " class=\"" . $classes . "\"";
143  }
144 
145  foreach ($this->elementsAndHtml as $element) {
146  $renderedElements .= $element;
147  }
148 
149  return "<fieldset {$htmlAttributes}>" .
150  "<legend>{$label}</legend>{$renderedElements}" .
151  "</fieldset>\n";
152  }
153 }
154 
155 /* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition: element.php:193
addElement($type, $name, $parameters)
Generates sub-elements.
Definition: fieldset.php:89
setDefaults()
collects initial values across subclasses.
Definition: fieldset.php:66
__toString()
Renders the fieldset to HTML code.
Definition: fieldset.php:131
htmlClasses()
Returns string of the elements&#39; HTML-classes, separated by spaces.
Definition: fieldset.php:107
The fieldset class holds HTML-fieldset specific attributes and methods.
Definition: fieldset.php:50
container element base class
Definition: container.php:21