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

Commit

Permalink
Merge pull request #53 from JerseyMilker/inline-radio-checkbox-chaining
Browse files Browse the repository at this point in the history
Inline Checkbox/Radio Fixes & Improvements
  • Loading branch information
adamwathan committed Jun 11, 2015
2 parents 469464a + a39cc97 commit 4490aba
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 30 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -10,7 +10,7 @@
"license": "MIT",
"require": {
"php": ">=5.4.0",
"adamwathan/form": "0.6.*"
"adamwathan/form": "0.7.*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
Expand Down
33 changes: 10 additions & 23 deletions src/AdamWathan/BootForms/BasicFormBuilder.php
Expand Up @@ -74,6 +74,11 @@ public function checkbox($label, $name)
return $this->checkGroup($label, $name, $control);
}

public function inlineCheckbox($label, $name)
{
return $this->checkbox($label, $name)->inline();
}

protected function checkGroup($label, $name, $control)
{
$checkGroup = $this->buildCheckGroup($label, $name, $control);
Expand Down Expand Up @@ -104,6 +109,11 @@ public function radio($label, $name, $value = null)
return $this->radioGroup($label, $name, $control);
}

public function inlineRadio($label, $name, $value = null)
{
return $this->radio($label, $name, $value)->inline();
}

protected function radioGroup($label, $name, $control)
{
$checkGroup = $this->buildCheckGroup($label, $name, $control);
Expand All @@ -117,29 +127,6 @@ public function textarea($label, $name)
return $this->formGroup($label, $name, $control);
}

public function inlineCheckbox($label, $name, $checked = false)
{
$label = $this->builder->label($label)->addClass('checkbox-inline');
$control = $this->builder->checkbox($name);

if ($checked) {
$control->check();
} else {
$control->uncheck();
}

return $label->after($control);
}

public function inlineRadio($label, $name, $value = null)
{
$value = $value ?: $label;
$label = $this->builder->label($label)->addClass('radio-inline');
$control = $this->builder->radio($name, $value);

return $label->after($control);
}

public function date($label, $name, $value = null)
{
$control = $this->builder->date($name)->value($value);
Expand Down
17 changes: 16 additions & 1 deletion src/AdamWathan/BootForms/Elements/CheckGroup.php
Expand Up @@ -6,14 +6,19 @@
class CheckGroup extends FormGroup
{
protected $label;
protected $inline = false;

public function __construct(Label $label)
{
$this->label = $label;
}

public function render()
{
{
if ($this->inline === true) {
return $this->label->render();
}

$html = '<div';
$html .= $this->renderAttributes();
$html .= '>';
Expand All @@ -25,6 +30,16 @@ public function render()
return $html;
}

public function inline()
{
$this->inline = true;

$class = $this->control()->getAttribute('type') . '-inline';
$this->label->removeClass('control-label')->addClass($class);

return $this;
}

public function control()
{
return $this->label->getControl();
Expand Down
2 changes: 1 addition & 1 deletion src/AdamWathan/BootForms/Elements/FormGroup.php
Expand Up @@ -24,7 +24,7 @@ public function render()
$html .= $this->label;
$html .= $this->control;
$html .= $this->renderHelpBlock();

$html .= '</div>';

return $html;
Expand Down
8 changes: 7 additions & 1 deletion src/AdamWathan/BootForms/Elements/GroupWrapper.php
Expand Up @@ -21,7 +21,7 @@ public function helpBlock($text)
$this->formGroup->helpBlock($text);
return $this;
}

public function __toString()
{
return $this->render();
Expand All @@ -39,6 +39,12 @@ public function hideLabel()
return $this;
}

public function inline()
{
$this->formGroup->inline();
return $this;
}

public function __call($method, $parameters)
{
call_user_func_array(array($this->formGroup->control(), $method), $parameters);
Expand Down
54 changes: 51 additions & 3 deletions tests/BasicFormBuilderTest.php
Expand Up @@ -343,20 +343,69 @@ public function testRenderTextareaWithError()
$this->assertEquals($expected, $result);
}

public function testRenderInlineCheckbox()
public function testRenderInlineCheckboxFallback()
{
$expected = '<label class="checkbox-inline"><input type="checkbox" name="terms" value="1">Agree to Terms</label>';
$result = $this->form->inlineCheckbox('Agree to Terms', 'terms')->render();
$this->assertEquals($expected, $result);
}

public function testRenderInlineRadio()
public function testRenderInlineCheckboxFallbackWithChaining()
{
$expected = '<label class="checkbox-inline"><input type="checkbox" name="DJ" value="meal" chain="link" checked="checked">Checkit!</label>';
$result = $this->form->inlineCheckbox('Checkit!', 'DJ')->value('meal')->chain('link')->check()->render();
$this->assertEquals($expected, $result);
}

public function testRenderInlineCheckboxModifier()
{
$expected = '<label class="checkbox-inline"><input type="checkbox" name="terms" value="1">Agree to Terms</label>';
$result = $this->form->checkbox('Agree to Terms', 'terms')->inline()->render();
$this->assertEquals($expected, $result);
}

public function testRenderInlineCheckboxModifierWithChaining()
{
$expected = '<label class="checkbox-inline"><input type="checkbox" name="DJ" value="meal" chain="link" checked="checked">Checkit!</label>';
$result = $this->form->checkbox('Checkit!', 'DJ')->inline()->value('meal')->chain('link')->check()->render();
$this->assertEquals($expected, $result);
}

public function testRenderInlineRadioFallback()
{
$expected = '<label class="radio-inline"><input type="radio" name="color" value="Red">Red</label>';
$result = $this->form->inlineRadio('Red', 'color')->render();
$this->assertEquals($expected, $result);
}

public function testRenderInlineRadioFallbackWithChaining()
{
$expected = '<label class="radio-inline"><input type="radio" name="colour" value="Canada Red" chain="link" checked="checked">Canada Red</label>';
$result = $this->form->inlineRadio('Canada Red', 'colour')->chain('link')->check()->render();
$this->assertEquals($expected, $result);
}

public function testRenderInlineRadioModifier()
{
$expected = '<label class="radio-inline"><input type="radio" name="color" value="Red">Red</label>';
$result = $this->form->radio('Red', 'color')->inline()->render();
$this->assertEquals($expected, $result);
}

public function testRenderInlineRadioModifierWithChaining()
{
$expected = '<label class="radio-inline"><input type="radio" name="colour" value="Canada Red" chain="link" checked="checked">Canada Red</label>';
$result = $this->form->radio('Canada Red', 'colour')->inline()->chain('link')->check()->render();
$this->assertEquals($expected, $result);
}

public function testRenderInlineModifierOnUnsupportedElement()
{
$expected = '<div class="form-group"><label class="control-label" for="name">Name</label><input type="text" name="name" id="name" class="form-control" inline="inline"></div>';
$result = $this->form->text('Name', 'name')->inline()->render();
$this->assertEquals($expected, $result);
}

public function testFormOpen()
{
$expected = '<form method="POST" action="">';
Expand Down Expand Up @@ -465,7 +514,6 @@ public function testBindObject()
$this->assertEquals($expected, $result);
}


public function testCanHideLabels()
{
$expected = '<div class="form-group"><label class="control-label sr-only" for="email">Email</label><input type="text" name="email" id="email" class="form-control"></div>';
Expand Down

0 comments on commit 4490aba

Please sign in to comment.