diff --git a/src/AbstractForm.php b/src/AbstractForm.php index c845020..52f13a1 100644 --- a/src/AbstractForm.php +++ b/src/AbstractForm.php @@ -40,6 +40,12 @@ abstract class AbstractForm extends Child implements \ArrayAccess */ protected $fields = []; + /** + * Field group headers + * @var array + */ + protected $headers = []; + /** * Filters * @var array @@ -211,6 +217,74 @@ public function clearFilters() return $this; } + /** + * Add field group header + * + * @param string $header + * @param int $weight + * @param int $position + * @return AbstractForm + */ + public function addHeader($header, $weight = 1, $position = null) + { + $weight = (int)$weight; + + if ($weight > 6) { + $weight = 6; + } + if ($weight < 1) { + $weight = 1; + } + + if (null !== $position) { + $this->headers[(int)$position] = [ + 'header' => $header, + 'weight' => $weight + ]; + } else { + $this->headers[] = [ + 'header' => $header, + 'weight' => $weight + ]; + } + + return $this; + } + + /** + * Remove field group header + * + * @param string $header + * @return AbstractForm + */ + public function removeHeader($header) + { + foreach ($this->headers as $key => $value) { + if ($value['header'] == $header) { + unset($this->headers[$key]); + } + } + + return $this; + } + + /** + * Remove field group header + * + * @param int $position + * @return AbstractForm + */ + public function removeHeaderByPosition($position) + { + $key = (int)$position; + + if (isset($this->headers[$key])) { + unset($this->headers[$key]); + } + + return $this; + } + /** * Get the form template for the render method to utilize. * diff --git a/src/Form.php b/src/Form.php index c67cea5..6f283ed 100644 --- a/src/Form.php +++ b/src/Form.php @@ -738,6 +738,13 @@ protected function renderWithoutTemplate() $dl->setAttribute('id', $id . '-' . $i); $dl->setAttribute('class', $id); + if (isset($this->headers[$i - 1])) { + $fieldGroupHeader = new Child('h' . $this->headers[$i - 1]['weight'], $this->headers[$i - 1]['header']); + $fieldGroupHeader->setAttribute('class', $id . '-header'); + $fieldGroupHeader->setIndent(($this->getIndent() . ' ')); + $dl->addChild($fieldGroupHeader); + } + // Loop through the children and create and attach the appropriate DT and DT elements, with labels where applicable. foreach ($children as $child) { if ($child->getNodeName() == 'fieldset') { @@ -759,6 +766,13 @@ protected function renderWithoutTemplate() $dl = new Child('dl', null, null, false, $this->getIndent()); $dl->setAttribute('id', $id . '-' . $i); $dl->setAttribute('class', $id); + + if (isset($this->headers[$i - 1])) { + $fieldGroupHeader = new Child('h' . $this->headers[$i - 1]['weight'], $this->headers[$i - 1]['header']); + $fieldGroupHeader->setAttribute('class', $id . '-header'); + $fieldGroupHeader->setIndent(($this->getIndent() . ' ')); + $dl->addChild($fieldGroupHeader); + } } } diff --git a/tests/FormTest.php b/tests/FormTest.php index 45d788b..b45ea2d 100644 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -518,6 +518,9 @@ public function testRenderForm() ] ]); + $form->addHeader('Test Header 1'); + $form->addHeader('Test Header 2', 2); + $form->getElement('colors')->setLabelAttributes([ 'class' => 'label-class' ]); @@ -540,6 +543,8 @@ public function testRenderForm() $this->assertContains('enctype="multipart/form-data"', $result); $this->assertContains('enctype="multipart/form-data"', $form->renderForm(true)); $this->assertContains('enctype="multipart/form-data"', $string); + $this->assertContains('Test Header 1', $form->renderForm(true)); + $this->assertContains('Test Header 2', $form->renderForm(true)); } public function testRenderFormNoElementsException()