Skip to content

Commit cc6f082

Browse files
author
Tortue Torche
committed
[L4] Support Bootstrap 3 inline checkbox and radio.
With the help of CSS classes checkbox-inline and radio-inline. Should fix formers#248
1 parent 420de4f commit cc6f082

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

src/Former/Framework/TwitterBootstrap3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ protected function setFieldWidths($labelWidths)
185185
public function getFieldClasses(Field $field, $classes)
186186
{
187187
// Add inline class for checkables
188-
if ($field->isCheckable() and in_array('inline', $classes)) {
188+
if ($field->isCheckable() and count(array_intersect(['inline', 'checkbox-inline', 'radio-inline'], $classes)) > 0) {
189189
$field->inline();
190190
}
191191

src/Former/Traits/Checkable.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,18 @@ protected function createCheckable($item, $fallbackValue = 1)
309309
}
310310

311311
// If inline items, add class
312-
$isInline = $this->inline ? ' inline' : null;
312+
$isInline = null;
313+
if ($this->inline) {
314+
$inlineClass = 'inline';
315+
if ($this->app['former.framework']->current() === 'TwitterBootstrap3') {
316+
if ($this->isOfType('checkbox', 'checkboxes')) {
317+
$inlineClass = 'checkbox-'.$inlineClass;
318+
} elseif ($this->isOfType('radio', 'radios')) {
319+
$inlineClass = 'radio-'.$inlineClass;
320+
}
321+
}
322+
$isInline = ' '.$inlineClass;
323+
}
313324

314325
// Merge custom attributes with global attributes
315326
$attributes = array_merge($this->attributes, $attributes);

tests/Fields/CheckboxTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ private function matchCheckbox($name = 'foo', $label = null, $value = 1, $inline
3030
'for' => $name,
3131
'class' => 'checkbox',
3232
);
33-
if ($inline) $labelAttr['class'] .= ' inline';
33+
if ($inline) {
34+
$labelAttr['class'] .= $this->former->framework() === 'TwitterBootstrap3' ? ' checkbox-inline' : ' inline';
35+
}
3436
if (!$checked) unset($checkAttr['checked']);
3537

3638
$radio = '<input'.$this->attributes($checkAttr).'>';
@@ -111,6 +113,19 @@ public function testCanCreateInlineCheckboxes()
111113
$this->assertEquals($matcher, $checkboxes2);
112114
}
113115

116+
public function testCanCreateInlineCheckboxesTwitterBootstrap3()
117+
{
118+
$this->former->framework('TwitterBootstrap3');
119+
120+
$checkboxes1 = $this->former->inline_checkboxes('foo')->checkboxes('foo', 'bar')->__toString();
121+
$this->resetLabels();
122+
$checkboxes2 = $this->former->checkboxes('foo')->inline()->checkboxes('foo', 'bar')->__toString();
123+
$matcher = $this->formGroup($this->matchCheckbox('foo_0', 'Foo', 1, true).$this->matchCheckbox('foo_1', 'Bar', 1, true));
124+
125+
$this->assertEquals($matcher, $checkboxes1);
126+
$this->assertEquals($matcher, $checkboxes2);
127+
}
128+
114129
public function testCanCreateStackedCheckboxes()
115130
{
116131
$checkboxes1 = $this->former->stacked_checkboxes('foo')->checkboxes('foo', 'bar')->__toString();

tests/Fields/RadioTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ private function matchRadio($name = 'foo', $label = null, $value = 1, $inline =
3030
'for' => $name,
3131
'class' => 'radio',
3232
);
33-
if ($inline) $labelAttr['class'] .= ' inline';
33+
if ($inline) {
34+
$labelAttr['class'] .= $this->former->framework() === 'TwitterBootstrap3' ? ' radio-inline' : ' inline';
35+
}
3436
if (!$checked) unset($radioAttr['checked']);
3537

3638
$radio = '<input'.$this->attributes($radioAttr).'>';
@@ -101,6 +103,20 @@ public function testInline()
101103
$this->assertEquals($matcher, $radios2);
102104
}
103105

106+
public function testInlineTwitterBootstrap3()
107+
{
108+
$this->former->framework('TwitterBootstrap3');
109+
110+
$radios1 = $this->former->inline_radios('foo')->radios('foo', 'bar')->__toString();
111+
$this->resetLabels();
112+
$radios2 = $this->former->radios('foo')->inline()->radios('foo', 'bar')->__toString();
113+
114+
$matcher = $this->formGroup($this->matchRadio('foo', 'Foo', 0, true).$this->matchRadio('foo2', 'Bar', 1, true));
115+
116+
$this->assertEquals($matcher, $radios1);
117+
$this->assertEquals($matcher, $radios2);
118+
}
119+
104120
public function testStacked()
105121
{
106122
$radios1 = $this->former->stacked_radios('foo')->radios('foo', 'bar')->__toString();

tests/TestCases/FormerTests.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,19 @@ protected function controlGroup($input = '<input type="text" name="foo" id="foo"
258258
return '<div class="control-group">'.$label.'<div class="controls">'.$input.'</div></div>';
259259
}
260260

261+
/**
262+
* Matches a Form Group
263+
*
264+
* @param string $input
265+
* @param string $label
266+
*
267+
* @return boolean
268+
*/
269+
protected function formGroup($input = '<input type="text" name="foo" id="foo">', $label = '<label for="foo" class="control-label col-lg-2 col-sm-4">Foo</label>')
270+
{
271+
return '<div class="form-group">'.$label.'<div class="col-lg-10 col-sm-8">'.$input.'</div></div>';
272+
}
273+
261274
/**
262275
* Matches a required Control Group
263276
*

0 commit comments

Comments
 (0)