Skip to content

Commit

Permalink
fixed label association when input has a user-defined id attribute (f…
Browse files Browse the repository at this point in the history
…ixes #72)
  • Loading branch information
Sam authored and Sam committed Apr 27, 2012
1 parent 4955070 commit f61ba42
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
11 changes: 11 additions & 0 deletions widgets/input/BootInput.php
Expand Up @@ -202,6 +202,17 @@ protected function getAppend($htmlOptions = array())
else
return '';
}

/**
* Returns the id that should be used for the specified attribute
* @param string $attribute the attribute
* @return string the id
*/
protected function getAttributeId($attribute)
{
return isset($this->htmlOptions['id']) ? $this->htmlOptions['id'] :
CHtml::getIdByName(CHtml::resolveName($this->model, $attribute));
}

/**
* Returns the input container CSS classes.
Expand Down
5 changes: 4 additions & 1 deletion widgets/input/BootInputHorizontal.php
Expand Up @@ -37,6 +37,9 @@ protected function getLabel($htmlOptions = array())
$htmlOptions['class'] .= ' '.$classes;
else
$htmlOptions['class'] = $classes;

if(isset($this->htmlOptions['id']))
$htmlOptions['for'] = $this->htmlOptions['id'];

return parent::getLabel($htmlOptions);
}
Expand All @@ -49,7 +52,7 @@ protected function checkBox()
{
$attribute = $this->attribute;
echo '<div class="controls">';
echo '<label class="checkbox" for="'.CHtml::getIdByName(CHtml::resolveName($this->model, $attribute)).'">';
echo '<label class="checkbox" for="'.$this->getAttributeId($attribute).'">';
echo $this->form->checkBox($this->model, $attribute, $this->htmlOptions).PHP_EOL;
echo $this->model->getAttributeLabel($attribute);
echo $this->getError().$this->getHint();
Expand Down
2 changes: 1 addition & 1 deletion widgets/input/BootInputVertical.php
Expand Up @@ -35,7 +35,7 @@ protected function captcha()
protected function checkBox()
{
$attribute = $this->attribute;
echo '<label class="checkbox" for="'.CHtml::getIdByName(CHtml::resolveName($this->model, $attribute)).'">';
echo '<label class="checkbox" for="'.$this->getAttributeId($attribute).'">';
echo $this->form->checkBox($this->model, $this->attribute, $this->htmlOptions).PHP_EOL;
echo $this->model->getAttributeLabel($attribute);
echo $this->getError().$this->getHint();
Expand Down

0 comments on commit f61ba42

Please sign in to comment.