Skip to content

Commit

Permalink
ids of radio buttons were not unique.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebacker committed Nov 28, 2018
1 parent 8ac7e09 commit 7b8e78f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
18 changes: 17 additions & 1 deletion src/TypiCMS/BootForms/BasicFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,27 @@ public function inlineRadio($label, $name, $value = null)

protected function radioGroup($label, $name, $control)
{
$checkGroup = $this->buildCheckGroup($label, $name, $control);
$checkGroup = $this->buildRadioGroup($label, $name, $control);

return $this->wrap($checkGroup);
}

protected function buildRadioGroup($label, $name, $control)
{
$id = $name.'_'.strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '_', $control->getAttribute('value'))));
$label = $this->builder->label($label, $name)->addClass('form-check-label')->forId($id);
$control->id($id)->addClass('form-check-input');

$checkGroup = new CheckGroup($label, $control);

if ($this->builder->hasError($name)) {
$checkGroup->invalidFeedback($this->builder->getError($name));
$control->addClass('is-invalid');
}

return $checkGroup;
}

public function textarea($label, $name)
{
$control = $this->builder->textarea($name);
Expand Down
14 changes: 7 additions & 7 deletions tests/BasicFormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function testRenderCheckboxChecked()

public function testRenderRadio()
{
$expected = '<div class="form-check"><input type="radio" name="color" value="red" id="color" class="form-check-input"><label class="form-check-label" for="color">Red</label></div>';
$expected = '<div class="form-check"><input type="radio" name="color" value="red" id="color_red" class="form-check-input"><label class="form-check-label" for="color_red">Red</label></div>';
$result = $this->form->radio('Red', 'color', 'red')->render();
$this->assertEquals($expected, $result);
}
Expand All @@ -281,7 +281,7 @@ public function testRenderRadioWithError()
$errorStore->shouldReceive('getError')->andReturn('Sample error');

$this->builder->setErrorStore($errorStore);
$expected = '<div class="form-check"><input type="radio" name="color" value="red" id="color" class="form-check-input is-invalid"><label class="form-check-label" for="color">Red</label><div class="invalid-feedback">Sample error</div></div>';
$expected = '<div class="form-check"><input type="radio" name="color" value="red" id="color_red" class="form-check-input is-invalid"><label class="form-check-label" for="color_red">Red</label><div class="invalid-feedback">Sample error</div></div>';
$result = $this->form->radio('Red', 'color', 'red')->render();
$this->assertEquals($expected, $result);
}
Expand All @@ -294,7 +294,7 @@ public function testRenderRadioWithOldInput()

$this->builder->setOldInputProvider($oldInput);

$expected = '<div class="form-check"><input type="radio" name="color" value="red" id="color" class="form-check-input" checked="checked"><label class="form-check-label" for="color">Red</label></div>';
$expected = '<div class="form-check"><input type="radio" name="color" value="red" id="color_red" class="form-check-input" checked="checked"><label class="form-check-label" for="color_red">Red</label></div>';
$result = $this->form->radio('Red', 'color', 'red')->render();
$this->assertEquals($expected, $result);
}
Expand Down Expand Up @@ -374,28 +374,28 @@ public function testRenderInlineCheckboxModifierWithChaining()

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

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

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

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

0 comments on commit 7b8e78f

Please sign in to comment.