Skip to content

Commit b6633e6

Browse files
committed
Merge pull request formers#255 from tortuetorche/patch-1
[L4] Support Bootstrap 3 inline checkbox and radio.
2 parents e394d70 + afbc499 commit b6633e6

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

src/Former/Framework/TwitterBootstrap3.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ public function errorState()
147147
return 'has-error';
148148
}
149149

150+
/**
151+
* Returns corresponding inline class of a field
152+
*
153+
* @param Field $field
154+
*
155+
* @return string
156+
*/
157+
public function getInlineLabelClass($field)
158+
{
159+
$inlineClass = parent::getInlineLabelClass($field);
160+
if ($field->isOfType('checkbox', 'checkboxes')) {
161+
$inlineClass = 'checkbox-'.$inlineClass;
162+
} elseif ($field->isOfType('radio', 'radios')) {
163+
$inlineClass = 'radio-'.$inlineClass;
164+
}
165+
166+
return $inlineClass;
167+
}
168+
150169
/**
151170
* Set the fields width from a label width
152171
*

src/Former/Traits/Checkable.php

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

311311
// If inline items, add class
312-
$isInline = $this->inline ? ' inline' : null;
312+
$isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null;
313313

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

src/Former/Traits/Framework.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ public function errorState()
147147
return 'error';
148148
}
149149

150+
151+
/**
152+
* Returns corresponding inline class of a field
153+
*
154+
* @param Field $field
155+
*
156+
* @return string
157+
*/
158+
public function getInlineLabelClass($field)
159+
{
160+
return 'inline';
161+
}
162+
150163
/**
151164
* Set framework defaults from its config file
152165
*/

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)