DateTimeWidget
extends BasicWidget
in package
Input widget class for generating a date time input widget.
This class is usually used internally by Cake\View\Helper\FormHelper
,
it but can be used to generate standalone date time inputs.
Table of Contents
Properties
- $_templates : StringTemplate
- Template instance.
- $defaults : array<string, mixed>
- Data defaults.
- $defaultStep : array<string, mixed>
- Step size for various input types.
- $formatMap : array<string|int, string>
- Formats for various input types.
Methods
- __construct() : mixed
- Constructor.
- render() : string
- Render a date / time form widget.
- secureFields() : array<string|int, string>
- Returns a list of fields that need to be secured for this widget.
- formatDateTime() : string
- Formats the passed date/time value into required string format.
- 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
Template instance.
protected
StringTemplate
$_templates
$defaults
Data defaults.
protected
array<string, mixed>
$defaults
= ['name' => '', 'val' => null, 'type' => 'datetime-local', 'escape' => true, 'timezone' => null, 'templateVars' => []]
$defaultStep
Step size for various input types.
protected
array<string, mixed>
$defaultStep
= ['datetime-local' => '1', 'date' => null, 'time' => '1', 'month' => null, 'week' => null]
If not set, defaults to browser default.
$formatMap
Formats for various input types.
protected
array<string|int, string>
$formatMap
= ['datetime-local' => 'Y-m-d\TH:i:s', 'date' => 'Y-m-d', 'time' => 'H:i:s', 'month' => 'Y-m', 'week' => 'Y-\WW']
Methods
__construct()
Constructor.
public
__construct(StringTemplate $templates) : mixed
Parameters
- $templates : StringTemplate
-
Templates list.
render()
Render a date / time form widget.
public
render(array<string, mixed> $data, ContextInterface $context) : string
Data supports the following keys:
-
name
The name attribute. -
val
The value attribute. -
escape
Set to false to disable escaping on all attributes. -
type
A valid HTML date/time input type. Defaults to "datetime-local". -
timezone
The timezone the input value should be converted to. -
step
The "step" attribute. Defaults to1
for "time" and "datetime-local" type inputs. You can set it tonull
orfalse
to prevent explicit step attribute being added in HTML. -
format
Adate()
function compatible datetime format string. By default, the widget will use a suitable format based on the input type and database type for the context. If an explicit format is provided, then no default value will be set for thestep
attribute, and it needs to be explicitly set if required.
All other keys will be converted into HTML attributes.
Parameters
- $data : array<string, mixed>
-
The data to build a file input with.
- $context : ContextInterface
-
The current form context.
Return values
string —HTML elements.
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.
formatDateTime()
Formats the passed date/time value into required string format.
protected
formatDateTime(DateTime|string|int|null $value, array<string, mixed> $options) : string
Parameters
- $value : DateTime|string|int|null
-
Value to deconstruct.
- $options : array<string, mixed>
-
Options for conversion.
Tags
Return values
stringmergeDefaults()
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.