Skip to content

Commit

Permalink
Add field group headers for rendering the form w/o a template
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Aug 8, 2016
1 parent 44e8386 commit 5411822
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/AbstractForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ abstract class AbstractForm extends Child implements \ArrayAccess
*/
protected $fields = [];

/**
* Field group headers
* @var array
*/
protected $headers = [];

/**
* Filters
* @var array
Expand Down Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]);
Expand All @@ -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</h1>', $form->renderForm(true));
$this->assertContains('Test Header 2</h2>', $form->renderForm(true));
}

public function testRenderFormNoElementsException()
Expand Down

0 comments on commit 5411822

Please sign in to comment.