Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Wrap horizontal check groups so attributes are added to the correct e…
Browse files Browse the repository at this point in the history
…lement
  • Loading branch information
adamwathan committed Sep 10, 2015
1 parent 4e5505e commit bac19cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/AdamWathan/BootForms/Elements/OffsetFormGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
use AdamWathan\Form\Elements\Element;
use AdamWathan\Form\Elements\Label;

class OffsetFormGroup extends Element
class OffsetFormGroup
{
protected $control;
protected $columnSizes;

public function __construct(Element $control, $columnSizes)
public function __construct($control, $columnSizes)
{
$this->control = $control;
$this->columnSizes = $columnSizes;
$this->addClass('form-group');
}

public function render()
{
$html = '<div';
$html .= $this->renderAttributes();
$html .= '>';
$html = '<div class="form-group">';
$html .= '<div class="' . $this->getControlClass() . '">';
$html .= $this->control;
$html .= '</div>';
Expand All @@ -43,7 +40,11 @@ protected function getControlClass()
$class .= sprintf('col-%s-offset-%s col-%s-%s ', $breakpoint, $offset, $breakpoint, $sizes[1]);
}
return trim($class);
}

public function __toString()
{
return $this->render();
}

public function __call($method, $parameters)
Expand Down
4 changes: 2 additions & 2 deletions src/AdamWathan/BootForms/HorizontalFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function checkbox($label, $name)
$control = $this->builder->checkbox($name);
$checkGroup = $this->checkGroup($label, $name, $control)->addClass('checkbox');

return new OffsetFormGroup($checkGroup, $this->columnSizes);
return new OffsetFormGroup($this->wrap($checkGroup), $this->columnSizes);
}

protected function checkGroup($label, $name, $control)
Expand All @@ -109,7 +109,7 @@ public function radio($label, $name, $value = null)
$control = $this->builder->radio($name, $value);
$checkGroup = $this->checkGroup($label, $name, $control)->addClass('radio');

return new OffsetFormGroup($checkGroup, $this->columnSizes);
return new OffsetFormGroup($this->wrap($checkGroup), $this->columnSizes);
}

public function file($label, $name, $value = null)
Expand Down
7 changes: 7 additions & 0 deletions tests/HorizontalFormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ public function testRenderCheckboxChecked()
$this->assertEquals($expected, $result);
}

public function testRenderCheckboxWithAdditionalAttributes()
{
$expected = '<div class="form-group"><div class="col-lg-offset-2 col-lg-10"><div class="checkbox"><label><input type="checkbox" name="terms" value="1" data-foo="bar">Agree to Terms</label></div></div></div>';
$result = $this->form->checkbox('Agree to Terms', 'terms')->attribute('data-foo', 'bar')->render();
$this->assertEquals($expected, $result);
}

public function testRenderRadio()
{
$expected = '<div class="form-group"><div class="col-lg-offset-2 col-lg-10"><div class="radio"><label><input type="radio" name="color" value="red">Red</label></div></div></div>';
Expand Down

0 comments on commit bac19cb

Please sign in to comment.