Generates a list of checkbox buttons.
A checkbox is a graphical control element that allows the user to choose one or more options from a predefined set of mutually exclusive options.
Instantiate the CheckboxList class using CheckboxList::widget().
$checkboxList = CheckboxList::widget();The generateField method is used to generate the field id and name for the HTML output.
Allowed arguments are:
modelName- The name of the model.fieldName- The name of the field.arrayable- Whether the field is an array. For default, it isfalse.
// generate field id and name
$checkboxList->generateField('model', 'field');Use the items method to add items to the checkbox list.
$checkboxList->items(
Checkbox::widget()->labelContent('Apple')->value(1),
Checkbox::widget()->labelContent('Banana')->value(2),
Checkbox::widget()->labelContent('Orange')->value(3),
);Use the provided methods to set specific attributes for the a element.
// setting class attribute
$checkboxList->class('container');Or, use the attributes method to set multiple attributes at once.
$checkboxList->attributes(['class' => 'container', 'style' => 'background-color: #eee;']);if you want to include the checked attribute, use the checked method.
$checkboxList->checked(true);Or add value for matching with the checkbox value attribute.
$checkboxList->checked('MyValue')->value('MyValue');For multiple checkboxes, use the checked method with the value to be checked.
$checkboxList->checked([1, 3]);if you want to include a label, use the labelContent method.
$checkboxList->labelContent('MyLabel');if you want to include the checkbox enclosed by the label element, use the enclosedByLabel method.
$checkboxList->enclosedByLabel(true);Adding hidden input
if you want to include a hidden input, use the uncheckValue method.
$checkboxList->uncheckValue('MyValue');Generate the HTML output using the render method, for simple instantiation.
$html = $checkboxList->render();Or, use the magic __toString method.
$html = (string) $checkboxList;Below are examples of common use cases:
// adding multiple attributes
$checkboxList->class('external')->value('Myvalue');Explore additional methods for setting various attributes such as lang, name, style, title, etc.
The template method allows you to customize the HTML output of the a element.
The following template tags are available:
| Tag | Description |
|---|---|
{label} |
The label element. |
{tag} |
The a element. |
// using a custom template
$checkboxList->template('<div>{tag}</div>');Refer to the Attribute Tests for comprehensive examples.
The following methods are available for setting attributes:
| Method | Description |
|---|---|
ariaDescribedBy() |
Set the aria-describedby attribute. |
ariaLabel() |
Set the aria-label attribute. |
attributes() |
Set multiple attributes at once. |
autofocus() |
Set the autofocus attribute. |
checked() |
Set the checked attribute. |
class() |
Set the class attribute. |
id() |
Set the id attribute. |
name() |
Set the name attribute. |
style() |
Set the style attribute. |
tabIndex() |
Set the tabindex attribute. |
Refer to the Custom Methods Tests for comprehensive examples.
The following methods are available for customizing the HTML output:
| Method | Description |
|---|---|
container() |
Set enabled or disabled for the container element. |
containerAttributes() |
Set attributes for the container element. |
containerClass() |
Set the class attribute for the container element. |
containerTag() |
Set the tag for the container element. |
generateField() |
Generate the field id and name for the HTML output. |
items() |
Set the items for the HTML output. |
render() |
Generates the HTML output. |
separator() |
Set the separator for the HTML output. |
template() |
Set the template for the HTML output. |
uncheckAttributes() |
Set the attributes for the hidden input tag. |
uncheckClass() |
Set the class attribute for the hidden input tag. |
uncheckValue() |
Set the value attribute for the hidden input tag. |
widget() |
Instantiates the CheckboxList::class. |
Refer to the Label Tests for comprehensive examples.
The following methods are available for customizing the HTML output:
| Method | Description |
|---|---|
enclosedByLabel() |
Set enabled or disabled for the enclosed-by-label element. |
labelAttributes() |
Set attributes for the label element. |
labelClass() |
Set the class attribute for the label element. |
labelContent() |
Set the content within the label element. |
labelFor() |
Set the for attribute for the label element. |
notLabel() |
Set disabled for the label element. |
Refer to the Validate Tests for comprehensive examples.
| Method | Description |
|---|---|
required() |
Set the required attribute. |