MultiCheckboxWidget
extends BasicWidget
in package
uses
IdGeneratorTrait
Input widget class for generating multiple checkboxes.
This class is usually used internally by Cake\View\Helper\FormHelper,
it but can be used to generate standalone multiple checkboxes.
Table of Contents
Properties
- $_idPrefix : string|null
- Prefix for id attribute.
- $_idSuffixes : array<string|int, string>
- A list of id suffixes used in the current rendering.
- $_label : LabelWidget
- Label widget instance.
- $_templates : StringTemplate
- StringTemplate instance.
- $defaults : array<string, mixed>
- Data defaults.
Methods
- __construct() : mixed
- Render multi-checkbox widget.
- render() : string
- Render multi-checkbox widget.
- secureFields() : array<string|int, string>
- Returns a list of fields that need to be secured for this widget.
- _clearIds() : void
- Clear the stored ID suffixes.
- _domId() : string
- Generate an ID suitable for use in an ID attribute.
- _id() : string
- Generate an ID attribute for an element.
- _idSuffix() : string
- Generate an ID suffix.
- _isDisabled() : bool
- Helper method for deciding what options are disabled.
- _isSelected() : bool
- Helper method for deciding what options are selected.
- _renderInput() : string
- Render a single checkbox & wrapper.
- _renderInputs() : array<string|int, string>
- Render the checkbox inputs.
- mergeDefaults() : array<string, mixed>
- Merge default values with supplied data.
- setMaxLength() : array<string, mixed>
- Set value for "maxlength" attribute if applicable.
- setRequired() : array<string, mixed>
- Set value for "required" attribute if applicable.
- setStep() : array<string, mixed>
- Set value for "step" attribute if applicable.
Properties
$_idPrefix
Prefix for id attribute.
protected
string|null
$_idPrefix
$_idSuffixes
A list of id suffixes used in the current rendering.
protected
array<string|int, string>
$_idSuffixes
= []
$_label
Label widget instance.
protected
LabelWidget
$_label
$_templates
StringTemplate instance.
protected
StringTemplate
$_templates
$defaults
Data defaults.
protected
array<string, mixed>
$defaults
= ['name' => '', 'escape' => true, 'options' => [], 'disabled' => null, 'val' => null, 'idPrefix' => null, 'templateVars' => [], 'label' => true]
Methods
__construct()
Render multi-checkbox widget.
public
__construct(StringTemplate $templates, LabelWidget $label) : mixed
This class uses the following templates:
-
checkboxRenders checkbox input controls. Accepts thename,valueandattrsvariables. -
checkboxWrapperRenders the containing div/element for a checkbox and its label. Accepts theinput, andlabelvariables. -
multicheckboxWrapperRenders a wrapper around grouped inputs. -
multicheckboxTitleRenders the title element for grouped inputs.
Parameters
- $templates : StringTemplate
-
Templates list.
- $label : LabelWidget
-
Label widget instance.
render()
Render multi-checkbox widget.
public
render(array<string, mixed> $data, ContextInterface $context) : string
Data supports the following options.
-
nameThe name attribute of the inputs to create.[]will be appended to the name. -
optionsAn array of options to create checkboxes out of. -
valEither a string/integer or array of values that should be checked. Can also be a complex options set. -
disabledEither a boolean or an array of checkboxes to disable. -
escapeSet to false to disable HTML escaping. -
optionsAn associative array of value=>labels to generate options for. -
idPrefixPrefix for generated ID attributes.
Options format
The options option can take a variety of data format depending on the complexity of HTML you want generated.
You can generate simple options using a basic associative array:
'options' => ['elk' => 'Elk', 'beaver' => 'Beaver']
If you need to define additional attributes on your option elements you can use the complex form for options:
'options' => [
['value' => 'elk', 'text' => 'Elk', 'data-foo' => 'bar'],
]
This form requires that both the value and text keys be defined.
If either is not set options will not be generated correctly.
Parameters
- $data : array<string, mixed>
-
The data to generate a checkbox set with.
- $context : ContextInterface
-
The current form context.
Return values
stringsecureFields()
Returns a list of fields that need to be secured for this widget.
public
secureFields(array<string|int, mixed> $data) : array<string|int, string>
Parameters
- $data : array<string|int, mixed>
-
The data to render.
Tags
Return values
array<string|int, string> —Array of fields to secure.
_clearIds()
Clear the stored ID suffixes.
protected
_clearIds() : void
_domId()
Generate an ID suitable for use in an ID attribute.
protected
_domId(string $value) : string
Parameters
- $value : string
-
The value to convert into an ID.
Return values
string —The generated id.
_id()
Generate an ID attribute for an element.
protected
_id(string $name, string $val) : string
Ensures that id's for a given set of fields are unique.
Parameters
- $name : string
-
The ID attribute name.
- $val : string
-
The ID attribute value.
Return values
string —Generated id.
_idSuffix()
Generate an ID suffix.
protected
_idSuffix(string $val) : string
Ensures that id's for a given set of fields are unique.
Parameters
- $val : string
-
The ID attribute value.
Return values
string —Generated id suffix.
_isDisabled()
Helper method for deciding what options are disabled.
protected
_isDisabled(string $key, mixed $disabled) : bool
Parameters
- $key : string
-
The key to test.
- $disabled : mixed
-
The disabled values.
Return values
bool_isSelected()
Helper method for deciding what options are selected.
protected
_isSelected(string $key, array<string|int, string>|string|int|false|null $selected) : bool
Parameters
- $key : string
-
The key to test.
- $selected : array<string|int, string>|string|int|false|null
-
The selected values.
Return values
bool_renderInput()
Render a single checkbox & wrapper.
protected
_renderInput(array<string, mixed> $checkbox, ContextInterface $context) : string
Parameters
- $checkbox : array<string, mixed>
-
An array containing checkbox key/value option pairs
- $context : ContextInterface
-
Context object.
Return values
string_renderInputs()
Render the checkbox inputs.
protected
_renderInputs(array<string, mixed> $data, ContextInterface $context) : array<string|int, string>
Parameters
- $data : array<string, mixed>
-
The data array defining the checkboxes.
- $context : ContextInterface
-
The current form context.
Return values
array<string|int, string> —An array of rendered inputs.
mergeDefaults()
Merge default values with supplied data.
protected
mergeDefaults(array<string, mixed> $data, ContextInterface $context) : array<string, mixed>
Parameters
- $data : array<string, mixed>
-
Data array
- $context : ContextInterface
-
Context instance.
Return values
array<string, mixed> —Updated data array.
setMaxLength()
Set value for "maxlength" attribute if applicable.
protected
setMaxLength(array<string, mixed> $data, ContextInterface $context, string $fieldName) : array<string, mixed>
Parameters
- $data : array<string, mixed>
-
Data array
- $context : ContextInterface
-
Context instance.
- $fieldName : string
-
Field name.
Return values
array<string, mixed> —Updated data array.
setRequired()
Set value for "required" attribute if applicable.
protected
setRequired(array<string, mixed> $data, ContextInterface $context, string $fieldName) : array<string, mixed>
Parameters
- $data : array<string, mixed>
-
Data array
- $context : ContextInterface
-
Context instance.
- $fieldName : string
-
Field name.
Return values
array<string, mixed> —Updated data array.
setStep()
Set value for "step" attribute if applicable.
protected
setStep(array<string, mixed> $data, ContextInterface $context, string $fieldName) : array<string, mixed>
Parameters
- $data : array<string, mixed>
-
Data array
- $context : ContextInterface
-
Context instance.
- $fieldName : string
-
Field name.
Return values
array<string, mixed> —Updated data array.