Class yii\bootstrap5\ToggleButtonGroup

Inheritanceyii\bootstrap5\ToggleButtonGroup » yii\bootstrap5\InputWidget » yii\widgets\InputWidget
Uses Traitsyii\bootstrap5\BootstrapWidgetTrait
Source Code https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/ToggleButtonGroup.php

ToggleButtonGroup allows rendering form inputs Checkbox/Radio toggle button groups.

You can use this widget in an ActiveForm using the yii\widgets\ActiveField::widget() method, for example like this:

<?= $form->field($model, 'item_id')->widget(\yii\bootstrap5\ToggleButtonGroup::class, [
    'type' => \yii\bootstrap5\ToggleButtonGroup::TYPE_CHECKBOX
    'items' => [
        'fooValue' => 'BarLabel',
        'barValue' => 'BazLabel'
    ]
]) ?>

See also https://getbootstrap.com/docs/5.1/components/buttons/#checkbox-and-radio-buttons.

Public Properties

Hide inherited properties

Property Type Description Defined By
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap5\BootstrapWidgetTrait
$clientOptions array|false The options for the underlying Bootstrap JS plugin/component. yii\bootstrap5\BootstrapWidgetTrait
$encodeLabels boolean Whether the items labels should be HTML-encoded. yii\bootstrap5\ToggleButtonGroup
$items array The data item used to generate the checkboxes. yii\bootstrap5\ToggleButtonGroup
$labelOptions yii\bootstrap5\ToggleButtonGroup
$type string Input type, can be TYPE_CHECKBOX or TYPE_RADIO yii\bootstrap5\ToggleButtonGroup

Public Methods

Hide inherited methods

Method Description Defined By
init() yii\bootstrap5\ToggleButtonGroup
renderItem() Default callback for checkbox/radio list item rendering. yii\bootstrap5\ToggleButtonGroup
run() yii\bootstrap5\ToggleButtonGroup

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap5\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin/component and the related events. yii\bootstrap5\BootstrapWidgetTrait

Constants

Hide inherited constants

Constant Value Description Defined By
TYPE_CHECKBOX 'checkbox' Checkbox type yii\bootstrap5\ToggleButtonGroup
TYPE_RADIO 'radio' Radio type yii\bootstrap5\ToggleButtonGroup

Property Details

Hide inherited properties

$encodeLabels public property

Whether the items labels should be HTML-encoded.

public boolean $encodeLabels true
$items public property

The data item used to generate the checkboxes. The array values are the labels, while the array keys are the corresponding checkbox or radio values.

public array $items = []
$labelOptions public property

See also:

  • \yii\bootstrap5\Html::checkbox()
  • \yii\bootstrap5\Html::radio()
public $labelOptions = [
    
'class' => [
        
'btn',
        
'btn-outline-secondary',
    ],
]
$type public property

Input type, can be TYPE_CHECKBOX or TYPE_RADIO

public string $type null

Method Details

Hide inherited methods

init() public method

public void init ( )

                public function init(): void
{
    parent::init();
    $this->registerPlugin('button');
    Html::addCssClass($this->options, [
        'widget' => 'btn-group',
    ]);
    $this->options['role'] = 'group';
}

            
registerClientEvents() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerClientEvents()

Registers JS event handlers that are listed in $clientEvents.

protected void registerClientEvents ( ?string $name null )
$name ?string

                protected function registerClientEvents(?string $name = null): void
{
    if (!empty($this->clientEvents)) {
        $id = $this->options['id'];
        $js = [];
        $appendix = ($name === 'dropdown') ? '.parentElement' : '';
        foreach ($this->clientEvents as $event => $handler) {
            $js[] = "document.getElementById('$id')$appendix.addEventListener('$event', $handler);";
        }
        $this->getView()->registerJs(implode("\n", $js));
    }
}

            
registerPlugin() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerPlugin()

Registers a specific Bootstrap plugin/component and the related events.

protected void registerPlugin ( string $name )
$name string

The name of the Bootstrap plugin

                protected function registerPlugin(string $name): void
{
    /**
     * @see https://github.com/twbs/bootstrap/blob/v5.2.0/js/index.esm.js
     */
    $jsPlugins = [
        'alert',
        'button',
        'carousel',
        'collapse',
        'dropdown',
        'modal',
        'offcanvas',
        'popover',
        'scrollspy',
        'tab',
        'toast',
        'tooltip',
    ];
    if (in_array($name, $jsPlugins, true)) {
        $view = $this->getView();
        BootstrapPluginAsset::register($view);
        // 'popover', 'toast' and 'tooltip' plugins not activates via data attributes
        if ($this->clientOptions !== false || in_array($name, ['popover', 'toast', 'tooltip'], true)) {
            $name = ucfirst($name);
            $id = $this->options['id'];
            $options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
            $view->registerJs("(new bootstrap.$name('#$id', $options));");
        }
        $this->registerClientEvents($name);
    }
}

            
renderItem() public method

Default callback for checkbox/radio list item rendering.

See also:

  • \yii\bootstrap5\Html::checkbox()
  • \yii\bootstrap5\Html::radio()
public string renderItem ( integer $index, string $label, string $name, boolean $checked, string $value )
$index integer

Item index.

$label string

Item label.

$name string

Input name.

$checked boolean

Whether value is checked or not.

$value string

Input value.

return string

Generated HTML.

                public function renderItem(int $index, string $label, string $name, bool $checked, string $value): string
{
    unset($index);
    $labelOptions = $this->labelOptions;
    $labelOptions['wrapInput'] = false;
    Html::addCssClass($labelOptions, [
        'widget' => 'btn',
    ]);
    $type = $this->type;
    if ($this->encodeLabels) {
        $label = Html::encode($label);
    }
    $options = [
        'label' => $label,
        'labelOptions' => $labelOptions,
        'autocomplete' => 'off',
        'value' => $value,
    ];
    Html::addCssClass($options, [
        'widget' => 'btn-check',
    ]);
    return Html::$type($name, $checked, $options);
}

            
run() public method

public string run ( )
throws \yii\base\InvalidConfigException

                public function run(): string
{
    if (!isset($this->options['item'])) {
        $this->options['item'] = [$this, 'renderItem'];
    }
    switch ($this->type) {
        case 'checkbox':
            if ($this->hasModel()) {
                return Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options);
            } else {
                return Html::checkboxList($this->name, $this->value, $this->items, $this->options);
            }
            // no break
        case 'radio':
            if ($this->hasModel()) {
                return Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options);
            } else {
                return Html::radioList($this->name, $this->value, $this->items, $this->options);
            }
            // no break
        default:
            throw new InvalidConfigException("Unsupported type '{$this->type}'");
    }
}