RadioWidget
extends BasicWidget
in package
uses
IdGeneratorTrait
Input widget class for generating a set of radio buttons.
This class is usually used internally by Cake\View\Helper\FormHelper
,
it but can be used to generate standalone radio buttons.
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 instance.
- $_templates : StringTemplate
- StringTemplate instance.
- $defaults : array<string, mixed>
- Data defaults.
Methods
- __construct() : mixed
- Constructor
- render() : string
- Render a set of radio buttons.
- 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
- Disabled attribute detection.
- _renderInput() : string
- Renders a single radio input and label.
- _renderLabel() : string|false
- Renders a label element for a given radio button.
- 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 instance.
protected
LabelWidget
$_label
$_templates
StringTemplate instance.
protected
StringTemplate
$_templates
$defaults
Data defaults.
protected
array<string, mixed>
$defaults
= ['name' => '', 'options' => [], 'disabled' => null, 'val' => null, 'escape' => true, 'label' => true, 'empty' => false, 'idPrefix' => null, 'templateVars' => []]
Methods
__construct()
Constructor
public
__construct(StringTemplate $templates, LabelWidget $label) : mixed
This class uses a few templates:
-
radio
Used to generate the input for a radio button. Can use the following variablesname
,value
,attrs
. -
radioWrapper
Used to generate the container element for the radio + input element. Can use theinput
andlabel
variables.
Parameters
- $templates : StringTemplate
-
Templates list.
- $label : LabelWidget
-
Label widget instance.
render()
Render a set of radio buttons.
public
render(array<string, mixed> $data, ContextInterface $context) : string
Data supports the following keys:
-
name
- Set the input name. -
options
- An array of options. See below for more information. -
disabled
- Either true or an array of inputs to disable. When true, the select element will be disabled. -
val
- A string of the option to mark as selected. -
label
- Either false to disable label generation, or an array of attributes for all labels. -
required
- Set to true to add the required attribute on all generated radios. -
idPrefix
Prefix for generated ID attributes.
Parameters
- $data : array<string, mixed>
-
The data to build radio buttons 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()
Disabled attribute detection.
protected
_isDisabled(array<string, mixed> $radio, array<string|int, mixed>|true|null $disabled) : bool
Parameters
- $radio : array<string, mixed>
-
Radio info.
- $disabled : array<string|int, mixed>|true|null
-
The disabled values.
Return values
bool_renderInput()
Renders a single radio input and label.
protected
_renderInput(string|int $val, array<string, mixed>|string $text, array<string, mixed> $data, ContextInterface $context) : string
Parameters
- $val : string|int
-
The value of the radio input.
- $text : array<string, mixed>|string
-
The label text, or complex radio type.
- $data : array<string, mixed>
-
Additional options for input generation.
- $context : ContextInterface
-
The form context
Return values
string_renderLabel()
Renders a label element for a given radio button.
protected
_renderLabel(array<string, mixed> $radio, array<string, mixed>|string|false $label, string $input, ContextInterface $context, bool $escape) : string|false
In the future this might be refactored into a separate widget as other input types (multi-checkboxes) will also need labels generated.
Parameters
- $radio : array<string, mixed>
-
The input properties.
- $label : array<string, mixed>|string|false
-
The properties for a label.
- $input : string
-
The input widget.
- $context : ContextInterface
-
The form context.
- $escape : bool
-
Whether to HTML escape the label.
Return values
string|false —Generated label.
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.