Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Commit

Permalink
Merge pull request #47 from tombevers/bugfix-label-rendering
Browse files Browse the repository at this point in the history
Label text should not be prepended when rendering an checkbox element in a horizontal form
  • Loading branch information
neilime committed Jan 17, 2014
2 parents 05d920f + 8ceabbf commit 4a71e5b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function renderElement(\Zend\Form\ElementInterface $oElement)
$sLayout = $oElement->getOption('twb-layout');

//Render label
$sLabelOpen = $sLabelClose = $sLabelContent = '';
$sLabelOpen = $sLabelClose = $sLabelContent = $sElementType = '';
if ($sLabelContent = $this->renderLabel($oElement)) {
//Multicheckbox elements have to be handled differently as the HTML standard does not allow nested labels. The semantic way is to group them inside a fieldset
$sElementType = $oElement->getAttribute('type');
Expand Down Expand Up @@ -210,6 +210,16 @@ protected function renderElement(\Zend\Form\ElementInterface $oElement)
$sClass .= ' col-' . $sColumSize;
}

// Checkbox elements are a special case. They don't need to render a label again
if ($sElementType === 'checkbox') {
return sprintf(
self::$horizontalLayoutFormat,
'',
$sClass,
$sElementContent
);
}

return sprintf(
self::$horizontalLayoutFormat,
$sLabelOpen . $sLabelContent . $sLabelClose,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<form action="" method="POST" role="form" class="form-horizontal"><div class="form-group "><div class=""><input name="input-text-disabled" type="text" placeholder="Disabled input here..." id="disabledInput" class="form-control" value=""></div></div>
<fieldset disabled="disabled" class="form-horizontal"><div class="form-group "><label class="control-label" for="disabledTextInput">Disabled input</label><div class=""><input name="fieldset-disabled[input-text-disabled]" type="text" placeholder="Disabled input" id="disabledTextInput" class="form-control" value=""></div></div>
<div class="form-group "><label class="control-label" for="disabled-select">Disabled select menu</label><div class=""><select name="fieldset-disabled[disabled-select]" id="disabled-select" class="form-control"><option value="">Disabled select</option></select></div></div>
<div class="form-group ">Can't check this<div class=""><div class="checkbox"><input type="hidden" name="fieldset-disabled[input-checkbox]" value="0"><label><input type="checkbox" name="fieldset-disabled[input-checkbox]" value="1">Can't check this</label></div></div></div>
<div class="form-group "><div class=""><div class="checkbox"><input type="hidden" name="fieldset-disabled[input-checkbox]" value="0"><label><input type="checkbox" name="fieldset-disabled[input-checkbox]" value="1">Can't check this</label></div></div></div>
<div class="form-group "><div class=""><button type="submit" name="fieldset-disabled[button-submit]" class="btn-primary btn" value="">Submit</button></div></div>
</fieldset></form>
2 changes: 1 addition & 1 deletion tests/_files/expected-forms/horizontal-form.phtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<form action="" method="POST" class="form-horizontal" role="form"><div class="form-group "><label class="col-sm-2 control-label" for="inputEmail1">Email</label><div class=" col-sm-10"><input name="input-email" type="email" placeholder="Enter email" id="inputEmail1" class="form-control" value=""></div></div>
<div class="form-group "><label class="col-sm-2 control-label" for="inputPassword1">Password</label><div class=" col-sm-10"><input name="input-password" type="password" placeholder="Password" id="inputPassword1" class="form-control" value=""></div></div>
<div class="form-group ">Remember me<div class=" col-sm-10 col-sm-offset-2"><div class="checkbox"><input type="hidden" name="input-checkbox" value="0"><label><input type="checkbox" name="input-checkbox" value="1">Remember me</label></div></div></div>
<div class="form-group "><div class=" col-sm-10 col-sm-offset-2"><div class="checkbox"><input type="hidden" name="input-checkbox" value="0"><label><input type="checkbox" name="input-checkbox" value="1">Remember me</label></div></div></div>
<div class="form-group "><div class=" col-sm-10 col-sm-offset-2"><button type="submit" name="button-submit" class="btn btn-default" value="">Sign in</button></div></div>
</form>

0 comments on commit 4a71e5b

Please sign in to comment.