CheckboxWidget
extends BasicWidget
in package
Input widget for creating checkbox widgets.
This class is usually used internally by Cake\View\Helper\FormHelper,
it but can be used to generate standalone checkboxes.
Table of Contents
Properties
- $_templates : StringTemplate
- StringTemplate instance.
- $defaults : array<string, mixed>
- Data defaults.
Methods
- __construct() : mixed
- Constructor.
- render() : string
- Render a checkbox element.
- secureFields() : array<string|int, string>
- Returns a list of fields that need to be secured for this widget.
- _isChecked() : bool
- Checks whether the checkbox should be checked.
- 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
$_templates
StringTemplate instance.
protected
StringTemplate
$_templates
$defaults
Data defaults.
protected
array<string, mixed>
$defaults
= ['name' => '', 'value' => 1, 'val' => null, 'disabled' => false, 'templateVars' => []]
Methods
__construct()
Constructor.
public
__construct(StringTemplate $templates) : mixed
Parameters
- $templates : StringTemplate
-
Templates list.
render()
Render a checkbox element.
public
render(array<string, mixed> $data, ContextInterface $context) : string
Data supports the following keys:
-
name- The name of the input. -
value- The value attribute. Defaults to '1'. -
val- The current value. If it matchesvaluethe checkbox will be checked. You can also use the 'checked' attribute to make the checkbox checked. -
disabled- Whether the checkbox should be disabled.
Any other attributes passed in will be treated as HTML attributes.
Parameters
- $data : array<string, mixed>
-
The data to create a checkbox with.
- $context : ContextInterface
-
The current form context.
Return values
string —Generated HTML string.
secureFields()
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.
_isChecked()
Checks whether the checkbox should be checked.
protected
_isChecked(array<string, mixed> $data) : bool
Parameters
- $data : array<string, mixed>
-
Data to look at and determine checked state.
Return values
boolmergeDefaults()
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.