# ToggleButtonGroup

> Renders a group of checkbox or radio toggle buttons for a model attribute or a named input.

[API reference](/docs/api/index.md) › [Bootstrap 6](/docs/extensions/bootstrap6/reference/index.md) › ToggleButtonGroup

| class | `yii2\extensions\bootstrap6\ToggleButtonGroup` |
| --- | --- |
| Extends | [`InputWidget`](/docs/extensions/bootstrap6/reference/inputwidget/index.md) » [`yii\widgets\InputWidget`](/docs/api/yii/widgets/inputwidget/index.md) » [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\ViewContextInterface`](/docs/api/yii/base/viewcontextinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Uses traits | [`AttributesTrait`](/docs/extensions/bootstrap6/reference/attributestrait/index.md), [`BootstrapWidgetTrait`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md) |
| Source | [`src/ToggleButtonGroup.php`](https://github.com/yii2-extensions/bootstrap6/blob/main/src/ToggleButtonGroup.php#L29) |

Renders a group of checkbox or radio toggle buttons for a model attribute or a named input.

Each item renders as `<label class="btn-check ..."><input type="checkbox" ...>Label</label>`; the input is nested in
the label, so no `id`/`for` pair is needed. With a model, a hidden input submits the empty value when nothing is
selected (configurable through the `unselect` key of [`$options`](/docs/api/yii/widgets/inputwidget/index.md#property-options)); it is disabled together with the inputs when
[`$inputOptions`](/docs/extensions/bootstrap6/reference/togglebuttongroup/index.md#property-inputOptions) carries `disabled`.

See also: <https://getbootstrap.com/docs/6.0/components/button-group/#toggle-buttons>.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-attribute"></span>[`$attribute`](/docs/api/yii/widgets/inputwidget/index.md#property-attribute) | `string\|null` | The model attribute that this widget is associated with. Default: `null`. | [`yii\widgets\InputWidget`](/docs/api/yii/widgets/inputwidget/index.md) |
| <span id="property-autoIdPrefix"></span>[`$autoIdPrefix`](/docs/api/yii/base/widget/index.md#property-autoIdPrefix) static | `string` | The prefix to the automatically generated widget IDs. Default: `'w'`. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-clientEvents"></span>[`$clientEvents`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md#property-clientEvents) | `array<string,` [`yii\web\JsExpression`](/docs/api/yii/web/jsexpression/index.md)`\|string>` | Event handlers attached to the widget element, as event name => JavaScript handler source. Default: `[]`. | [`BootstrapWidgetTrait`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md) |
| <span id="property-clientOptions"></span>[`$clientOptions`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md#property-clientOptions) | `array<string, mixed>\|false` | Options passed to the plugin constructor, or `false` to skip the JavaScript initialization. Default: `[]`. | [`BootstrapWidgetTrait`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md) |
| <span id="property-encodeLabels"></span>`$encodeLabels` | `bool` | Whether to HTML-encode the item labels. Default: `true`. | `ToggleButtonGroup` |
| <span id="property-field"></span>[`$field`](/docs/api/yii/widgets/inputwidget/index.md#property-field) | [`yii\widgets\ActiveField`](/docs/api/yii/widgets/activefield/index.md) | Active input field, which triggers this widget rendering. Default: `null`. Available since 2.0.11. | [`yii\widgets\InputWidget`](/docs/api/yii/widgets/inputwidget/index.md) |
| <span id="property-id"></span>[`$id`](/docs/api/yii/base/widget/index.md#property-id) | `string` | Id of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-inputOptions"></span>`$inputOptions` | `array<string, mixed>` | HTML attributes of every input tag, for example `['disabled' => true]`. Default: `[]`. | `ToggleButtonGroup` |
| <span id="property-items"></span>`$items` | `array<array-key, string>` | Toggle buttons as value => label pairs. Default: `[]`. | `ToggleButtonGroup` |
| <span id="property-labelOptions"></span>`$labelOptions` | `array<string, mixed>` | HTML attributes of every label (button) tag. Default: `[]`. | `ToggleButtonGroup` |
| <span id="property-model"></span>[`$model`](/docs/api/yii/widgets/inputwidget/index.md#property-model) | [`yii\base\Model`](/docs/api/yii/base/model/index.md)`\|null` | The data model that this widget is associated with. Default: `null`. | [`yii\widgets\InputWidget`](/docs/api/yii/widgets/inputwidget/index.md) |
| <span id="property-name"></span>[`$name`](/docs/api/yii/widgets/inputwidget/index.md#property-name) | `string\|null` | The input name. Default: `null`. | [`yii\widgets\InputWidget`](/docs/api/yii/widgets/inputwidget/index.md) |
| <span id="property-options"></span>[`$options`](/docs/api/yii/widgets/inputwidget/index.md#property-options) | `array` | The HTML attributes for the input tag. Default: `[]`. | [`yii\widgets\InputWidget`](/docs/api/yii/widgets/inputwidget/index.md) |
| <span id="property-size"></span>`$size` | [`yii2\extensions\bootstrap6\enum\Size`](/docs/extensions/bootstrap6/reference/enum/size/index.md)`\|string\|null` | Size modifier rendered as the `btn-group-{value}` class (`xs`, `sm`, or `lg`), or `null` for the default. Default: `null`. | `ToggleButtonGroup` |
| <span id="property-theme"></span>`$theme` | [`yii2\extensions\bootstrap6\enum\Theme`](/docs/extensions/bootstrap6/reference/enum/theme/index.md)`\|string\|null` | Theme color of the buttons rendered as the `theme-{value}` class, or `null` for the neutral color. Default: `null`. | `ToggleButtonGroup` |
| <span id="property-type"></span>`$type` | `string` | Input type, either [`TYPE_CHECKBOX`](/docs/extensions/bootstrap6/reference/togglebuttongroup/index.md#const-TYPE_CHECKBOX) or [`TYPE_RADIO`](/docs/extensions/bootstrap6/reference/togglebuttongroup/index.md#const-TYPE_RADIO). Default: `self::TYPE_CHECKBOX`. | `ToggleButtonGroup` |
| <span id="property-value"></span>[`$value`](/docs/api/yii/widgets/inputwidget/index.md#property-value) | `string` | The input value. Default: `null`. | [`yii\widgets\InputWidget`](/docs/api/yii/widgets/inputwidget/index.md) |
| <span id="property-variant"></span>`$variant` | [`yii2\extensions\bootstrap6\enum\ButtonVariant`](/docs/extensions/bootstrap6/reference/enum/buttonvariant/index.md)`\|string` | Button style class of the labels. Default: `\yii2\extensions\bootstrap6\enum\ButtonVariant::OUTLINE`. | `ToggleButtonGroup` |
| <span id="property-view"></span>[`$view`](/docs/api/yii/base/widget/index.md#property-view) | [`yii\web\View`](/docs/api/yii/web/view/index.md) | The view object that can be used to render views or view files. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-viewPath"></span>[`$viewPath`](/docs/api/yii/base/widget/index.md#property-viewPath) read-only | `string` | The directory containing the view files for this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-addAttributes"></span>[`addAttributes()`](/docs/extensions/bootstrap6/reference/attributestrait/index.md#method-addAttributes) | Adds a set of HTML attributes; attributes already set with the same name are replaced. | [`AttributesTrait`](/docs/extensions/bootstrap6/reference/attributestrait/index.md) |
| <span id="method-addClass"></span>[`addClass()`](/docs/extensions/bootstrap6/reference/attributestrait/index.md#method-addClass) | Adds one or more CSS classes to the existing classes; duplicated classes are collapsed. | [`AttributesTrait`](/docs/extensions/bootstrap6/reference/attributestrait/index.md) |
| <span id="method-addCssStyle"></span>[`addCssStyle()`](/docs/extensions/bootstrap6/reference/attributestrait/index.md#method-addCssStyle) | Adds CSS style properties to the `style` attribute. | [`AttributesTrait`](/docs/extensions/bootstrap6/reference/attributestrait/index.md) |
| <span id="method-afterRun"></span>[`afterRun()`](/docs/api/yii/base/widget/index.md#method-afterRun) | This method is invoked right after a widget is executed. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attribute"></span>[`attribute()`](/docs/extensions/bootstrap6/reference/attributestrait/index.md#method-attribute) | Sets an HTML attribute value. | [`AttributesTrait`](/docs/extensions/bootstrap6/reference/attributestrait/index.md) |
| <span id="method-beforeRun"></span>[`beforeRun()`](/docs/api/yii/base/widget/index.md#method-beforeRun) | This method is invoked right before the widget is executed. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-begin"></span>[`begin()`](/docs/api/yii/base/widget/index.md#method-begin) static | Begins a widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-class"></span>[`class()`](/docs/extensions/bootstrap6/reference/attributestrait/index.md#method-class) | Replaces the existing CSS classes with the specified ones. | [`AttributesTrait`](/docs/extensions/bootstrap6/reference/attributestrait/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-end"></span>[`end()`](/docs/api/yii/base/widget/index.md#method-end) static | Ends a widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getId"></span>[`getId()`](/docs/api/yii/base/widget/index.md#method-getId) | Returns the ID of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-getView"></span>[`getView()`](/docs/api/yii/base/widget/index.md#method-getView) | Returns the view object that can be used to render views or view files. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-getViewPath"></span>[`getViewPath()`](/docs/api/yii/base/widget/index.md#method-getViewPath) | Returns the directory containing the view files for this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) |  | `ToggleButtonGroup` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-render"></span>[`render()`](/docs/api/yii/base/widget/index.md#method-render) | Renders a view. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-renderFile"></span>[`renderFile()`](/docs/api/yii/base/widget/index.md#method-renderFile) | Renders a view file. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`run()`](#method-run) |  | `ToggleButtonGroup` |
| <span id="method-setId"></span>[`setId()`](/docs/api/yii/base/widget/index.md#method-setId) | Sets the ID of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-setView"></span>[`setView()`](/docs/api/yii/base/widget/index.md#method-setView) | Sets the view object to be used by this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-widget"></span>[`widget()`](/docs/api/yii/base/widget/index.md#method-widget) static | Creates a widget instance and runs it. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-hasModel"></span>[`hasModel()`](/docs/api/yii/widgets/inputwidget/index.md#method-hasModel) protected |  | [`yii\widgets\InputWidget`](/docs/api/yii/widgets/inputwidget/index.md) |
| <span id="method-pluginInitialization"></span>[`pluginInitialization()`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md#method-pluginInitialization) protected | Returns the JavaScript that initializes a plugin with its normalized constructor options. | [`BootstrapWidgetTrait`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md) |
| <span id="method-registerClientEvents"></span>[`registerClientEvents()`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md#method-registerClientEvents) protected | Registers the JavaScript event handlers listed in [`$clientEvents`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md#property-clientEvents) at [`View::POS_END`](/docs/api/yii/web/view/index.md#const-POS_END). | [`BootstrapWidgetTrait`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md) |
| <span id="method-registerPlugin"></span>[`registerPlugin()`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md#method-registerPlugin) protected | Registers the plugin asset, the plugin initialization when needed, and the [`$clientEvents`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md#property-clientEvents). | [`BootstrapWidgetTrait`](/docs/extensions/bootstrap6/reference/bootstrapwidgettrait/index.md) |
| <span id="method-renderInputHtml"></span>[`renderInputHtml()`](/docs/api/yii/widgets/inputwidget/index.md#method-renderInputHtml) protected | Render a HTML input tag. | [`yii\widgets\InputWidget`](/docs/api/yii/widgets/inputwidget/index.md) |
| [`renderItem()`](#method-renderItem) protected | Renders a toggle button. | `ToggleButtonGroup` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_INIT"></span>[`EVENT_INIT`](/docs/api/yii/base/widget/index.md#event-EVENT_INIT) | [`yii\base\Event`](/docs/api/yii/base/event/index.md) | An event that is triggered when the widget is initialized via [`init()`](/docs/api/yii/base/widget/index.md#method-init). Event name: `'init'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="event-EVENT_BEFORE_RUN"></span>[`EVENT_BEFORE_RUN`](/docs/api/yii/base/widget/index.md#event-EVENT_BEFORE_RUN) | [`yii\base\WidgetEvent`](/docs/api/yii/base/widgetevent/index.md) | An event raised right before executing a widget. Event name: `'beforeRun'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="event-EVENT_AFTER_RUN"></span>[`EVENT_AFTER_RUN`](/docs/api/yii/base/widget/index.md#event-EVENT_AFTER_RUN) | [`yii\base\WidgetEvent`](/docs/api/yii/base/widgetevent/index.md) | An event raised right after executing a widget. Event name: `'afterRun'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-TYPE_CHECKBOX"></span>`TYPE_CHECKBOX` | `'checkbox'` | Checkbox toggle buttons; the input name receives the `[]` suffix. | `ToggleButtonGroup` |
| <span id="const-TYPE_RADIO"></span>`TYPE_RADIO` | `'radio'` | Radio toggle buttons. | `ToggleButtonGroup` |
| <span id="const-TYPES"></span>`TYPES` | `[self::TYPE_CHECKBOX, self::TYPE_RADIO]` | Supported values of [`$type`](/docs/extensions/bootstrap6/reference/togglebuttongroup/index.md#property-type). | `ToggleButtonGroup` |

## Method details

### init() <span id="method-init"></span>

```php
public function init(): void
```

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the widget has neither a model attribute nor a name, or [`$size`](/docs/extensions/bootstrap6/reference/togglebuttongroup/index.md#property-size), [`$theme`](/docs/extensions/bootstrap6/reference/togglebuttongroup/index.md#property-theme), [`$type`](/docs/extensions/bootstrap6/reference/togglebuttongroup/index.md#property-type), or [`$variant`](/docs/extensions/bootstrap6/reference/togglebuttongroup/index.md#property-variant) is not supported.

### run() <span id="method-run"></span>

```php
public function run(): string
```

**Returns**

`string`

### renderItem() <span id="method-renderItem"></span>

```php
protected function renderItem(string $label, string $name, bool $checked, string $value): string
```

Renders a toggle button.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$label` | `string` | Item label, HTML-encoded when [`$encodeLabels`](/docs/extensions/bootstrap6/reference/togglebuttongroup/index.md#property-encodeLabels) is `true`. |
| `$name` | `string` | Input name. |
| `$checked` | `bool` | Whether the input is checked. |
| `$value` | `string` | Input value. |

**Returns**

`string`: Generated HTML.