Skip to content

Commit

Permalink
Customized the "id" attribute from the 2nd until the last options on …
Browse files Browse the repository at this point in the history
…Radio and MultiCheckbox inputs to allow a working connection with labels via the "for" attribute.
  • Loading branch information
rolandoisidoro committed Jul 7, 2018
1 parent 1b4bf4e commit b805632
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
13 changes: 8 additions & 5 deletions src/TwbsHelper/Form/View/Helper/FormMultiCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,9 @@ protected function renderOptions(MultiCheckbox $oElement, array $aOptions, array
$iCount = 0;

foreach ($aOptions as $sKey => $aOptionSpec) {
// Count number of options
$iCount++;

if ($iCount > 1 && array_key_exists('id', $aAttributes)) {
unset($aAttributes['id']);
}

$sValue = '';
$sLabel = '';
$aInputAttributes = $aAttributes;
Expand All @@ -80,6 +77,12 @@ protected function renderOptions(MultiCheckbox $oElement, array $aOptions, array
&& $aInputAttributes['selected']);
$bDisabled = (isset($aInputAttributes['disabled']) && $aInputAttributes['disabled']);

// Customize the 'id' input attribute to enable
// working 'for' label attribute on all options
if ($iCount > 1 && array_key_exists('id', $aAttributes)) {
$aInputAttributes['id'] .= $iCount;
}

if (is_scalar($aOptionSpec)) {
$aOptionSpec = [
'label' => $aOptionSpec,
Expand Down Expand Up @@ -140,7 +143,7 @@ protected function renderOptions(MultiCheckbox $oElement, array $aOptions, array
: $aOptionSpec['label_attributes'];
}

// Assign label for attribute with element id attribute when defined
// Assign label for attribute with defined element id attribute
if (empty($aLabelAttributes['for']) && ! empty($aInputAttributes['id'])) {
$aLabelAttributes['for'] = $aInputAttributes['id'];
}
Expand Down
23 changes: 16 additions & 7 deletions src/TwbsHelper/Form/View/Helper/FormRadio.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,32 @@ public function render(ElementInterface $oElement)
*/
protected function renderOptions(MultiCheckbox $oElement, array $aOptions, array $aSelectedOptions, array $aAttributes)
{
$iIterator = 0;
$iCount = 0;
$aGlobalLabelAttributes = $oElement->getLabelAttributes() ? : $this->labelAttributes;
$sMarkup = '';
$oLabelHelper = $this->getLabelHelper();
$aElementOptions = $oElement->getOptions();

foreach ($aOptions as $key => $aOptionspec) {
// Count number of options
$iCount++;

if (is_scalar($aOptionspec)) {
$aOptionspec = ['label' => $aOptionspec, 'value' => $key];
}

$iIterator++;
if ($iIterator > 1 && array_key_exists('id', $aAttributes)) {
unset($aAttributes['id']);
}

// Option attributes
$aInputAttributes = $aAttributes;
if (isset($aOptionspec['attributes'])) {
$aInputAttributes = \Zend\Stdlib\ArrayUtils::merge($aInputAttributes, $aOptionspec['attributes']);
}

// Customize the 'id' input attribute to enable
// working 'for' label attribute on all options
if ($iCount > 1 && array_key_exists('id', $aAttributes)) {
$aInputAttributes['id'] .= $iCount;
}

// Add BS4 form-check-input class if not set
$aInputAttributes['class'] = ! empty($aInputAttributes['class']) ? $aInputAttributes['class'] : '';
$aInputAttributes['class'] .= (false === strpos($aInputAttributes['class'], 'form-check-label')) ? ' form-check-input' : '';
Expand Down Expand Up @@ -131,12 +135,17 @@ protected function renderOptions(MultiCheckbox $oElement, array $aOptions, array
// Render option
$sOptionMarkup = sprintf('<input %s%s', $this->createAttributesString($aInputAttributes), $this->getInlineClosingBracket());

//Option label
// Option label
$sLabel = isset($aOptionspec['label']) ? $aOptionspec['label'] : '';

if ($sLabel) {
$aLabelAttributes = $aGlobalLabelAttributes;

// Assign label for attribute with defined element id attribute
if (empty($aLabelAttributes['for']) && ! empty($aInputAttributes['id'])) {
$aLabelAttributes['for'] = $aInputAttributes['id'];
}

// Add BS4 form-check-label class if not set
$aLabelAttributes['class'] = ! empty($aLabelAttributes['class']) ? $aLabelAttributes['class'] : '';
$aLabelAttributes['class'] .= (false === strpos($aLabelAttributes['class'], 'form-check-label')) ? ' form-check-label' : '';
Expand Down

0 comments on commit b805632

Please sign in to comment.