Skip to content

Commit

Permalink
Fix label() method and make more templates configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Jun 6, 2014
1 parent d1e0a54 commit 65e18af
Showing 1 changed file with 71 additions and 10 deletions.
81 changes: 71 additions & 10 deletions extensions/bootstrap/ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@
* The wrapper tag is only used for some layouts and form elements.
*
* Note that some elements use slightly different defaults for [[template]] and other options.
* In particular the elements are [[checkbox()]], [[checkboxList()]] and [[radioList()]].
* So to further customize these elements you may want to pass your custom options.
* You may want to override those predefined templates for checkboxes, radio buttons, checkboxLists
* and radioLists in the [[\yii\widgets\ActiveForm::fieldConfig|fieldConfig]] of the
* [[\yii\widgets\ActiveForm]]:
*
* - [[checkboxTemplate]] the template for checkboxes in default layout
* - [[radioTemplate]] the template for radio buttons in default layout
* - [[horizontalCheckboxTemplate]] the template for checkboxes in horizontal layout
* - [[horizontalRadioTemplate]] the template for radio buttons in horizontal layout
* - [[inlineCheckboxListTemplate]] the template for inline checkboxLists
* - [[inlineRadioListTemplate]] the template for inline radioLists
*
* Example:
*
Expand Down Expand Up @@ -108,6 +116,36 @@ class ActiveField extends \yii\widgets\ActiveField
*/
public $horizontalCssClasses;

/**
* @var string the template for checkboxes in default layout
*/
public $checkboxTemplate = "<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";

/**
* @var string the template for radios in default layout
*/
public $radioTemplate = "<div class=\"radio\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";

/**
* @var string the template for checkboxes in horizontal layout
*/
public $horizontalCheckboxTemplate = "{beginWrapper}\n<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";

/**
* @var string the template for radio buttons in horizontal layout
*/
public $horizontalRadioTemplate = "{beginWrapper}\n<div class=\"radio\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";

/**
* @var string the template for inline checkboxLists
*/
public $inlineCheckboxListTemplate = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";

/**
* @var string the template for inline radioLists
*/
public $inlineRadioListTemplate = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";

/**
* @var bool whether to render the error. Default is `true` except for layout `inline`.
*/
Expand Down Expand Up @@ -167,31 +205,53 @@ public function checkbox($options = [], $enclosedByLabel = true)
{
if ($enclosedByLabel) {
if (!isset($options['template'])) {
if ($this->form->layout === 'horizontal') {
$this->template = "{beginWrapper}\n<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";
Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
} else {
$this->template = "<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
}
$this->template = $this->form->layout === 'horizontal' ?
$this->horizontalCheckboxTemplate : $this->checkBoxTemplate;
} else {
$this->template = $options['template'];
unset($options['template']);
}
if ($this->form->layout === 'horizontal') {
Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
}
$this->labelOptions['class'] = null;
}

parent::checkbox($options, false);
return $this;
}

/**
* @inheritdoc
*/
public function radio($options = [], $enclosedByLabel = true)
{
if ($enclosedByLabel) {
if (!isset($options['template'])) {
$this->template = $this->form->layout === 'horizontal' ?
$this->horizontalRadioTemplate : $this->radioTemplate;
} else {
$this->template = $options['template'];
unset($options['template']);
}
if ($this->form->layout === 'horizontal') {
Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
}
$this->labelOptions['class'] = null;
}

parent::radio($options, false);
return $this;
}

/**
* @inheritdoc
*/
public function checkboxList($items, $options = [])
{
if ($this->inline) {
if (!isset($options['template'])) {
$this->template = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
$this->template = $this->inlineCheckboxListTemplate;
} else {
$this->template = $options['template'];
unset($options['template']);
Expand All @@ -214,7 +274,7 @@ public function radioList($items, $options = [])
{
if ($this->inline) {
if (!isset($options['template'])) {
$this->template = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
$this->template = $this->inlineRadioListTemplate;
} else {
$this->template = $options['template'];
unset($options['template']);
Expand All @@ -241,6 +301,7 @@ public function label($label = null, $options = [])
Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
}
} else {
$this->enableLabel = true;
$this->renderLabelParts($label, $options);
parent::label($label, $options);
}
Expand Down

0 comments on commit 65e18af

Please sign in to comment.