The input element with a type attribute whose value is checkbox represents a state or option that can be toggled.
Instantiate the Checkbox class using Checkbox::widget().
$checkbox = Checkbox::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
$checkbox->generateField('model', 'field');Use the provided methods to set specific attributes for the a element.
// setting class attribute
$checkbox->class('container');Or, use the attributes method to set multiple attributes at once.
$checkbox->attributes(['class' => 'container', 'style' => 'background-color: #eee;']);If you want to include value in the checkbox element, use the value method.
$checkbox->value('MyValue');if you want to include the checked attribute, use the checked method.
$checkbox->checked(true);Or add value for matching with the checkbox value attribute.
$checkbox->checked('MyValue')->value('MyValue');if you want to include a label, use the labelContent method.
$checkbox->labelContent('MyLabel');if you want to include the checkbox enclosed by the label element, use the enclosedByLabel method.
$checkbox->enclosedByLabel(true);Adding hidden input
if you want to include a hidden input, use the uncheckValue method.
$checkbox->uncheckValue('MyValue');Generate the HTML output using the render method, for simple instantiation.
$html = $checkbox->render();Or, use the magic __toString method.
$html = (string) $checkbox;Below are examples of common use cases:
// adding multiple attributes
$checkbox->class('external')->value('Myvalue');
// using data attributes
$checkbox->dataAttributes(['analytics' => 'trackClick']);Explore additional methods for setting various attributes such as lang, name, style, title, etc.
Use prefix and suffix methods to add text before and after the text tag, respectively.
// adding a prefix
$html = $checkbox->value('MyValue')->prefix('MyPrefix')->render();
// adding a suffix
$html = $checkbox->value('MyValue')->suffix('MySuffix')->render();The template method allows you to customize the HTML output of the a element.
The following template tags are available:
| Tag | Description |
|---|---|
{prefix} |
The prefix text. |
{tag} |
The a element. |
{suffix} |
The suffix text. |
// using a custom template
$checkbox->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. |
dataAttributes() |
Set multiple data-attributes at once. |
disabled() |
Set the disabled attribute. |
form() |
Set the form attribute. |
hidden() |
Set the hidden attribute. |
id() |
Set the id attribute. |
lang() |
Set the lang attribute. |
name() |
Set the name attribute. |
readOnly() |
Set the readonly attribute. |
style() |
Set the style attribute. |
tabIndex() |
Set the tabindex attribute. |
title() |
Set the title attribute. |
value() |
Set the value 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. |
prefix() |
Add text before the textarea element. |
prefixContainer() |
Set enabled or disabled for the prefix-container element. |
prefixContainerAttributes() |
Set attributes for the prefix-container element. |
prefixContainerClass() |
Set the class attribute for the prefix-container element. |
prefixContainerTag() |
Set the tag for the prefix-container element. |
render() |
Generates the HTML output. |
separator() |
Set the separator for the HTML output. |
suffix() |
Add text after the label element. |
suffixContainer() |
Set enabled or disabled for the suffix-container element. |
suffixContainerAttributes() |
Set attributes for the suffix-container element. |
suffixContainerClass() |
Set the class attribute for the suffix-container element. |
suffixContainerTag() |
Set the tag for the suffix-container element. |
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 Checkbox::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. |