Skip to content

Latest commit

 

History

History
170 lines (121 loc) · 7.5 KB

date.rst

File metadata and controls

170 lines (121 loc) · 7.5 KB

single: Forms; Fields; date

date Field Type

A field that allows the user to modify date information via a variety of different HTML elements.

The underlying data used for this field type can be a DateTime object, a string, a timestamp or an array. As long as the input option is set correctly, the field will take care of all of the details.

The field can be rendered as a single text box, three text boxes (month, day, and year) or three select boxes (see the widget option).

Underlying Data Type can be DateTime, string, timestamp, or array (see the input option)
Rendered as single text box or three select fields
Options
Overridden Options
Inherited options
Parent type field (if text), form otherwise
Class Symfony\\Component\\Form\\Extension\\Core\\Type\\DateType

Basic Usage

This field type is highly configurable, but easy to use. The most important options are input and widget.

Suppose that you have a publishedAt field whose underlying date is a DateTime object. The following configures the date type for that field as three different choice fields:

$builder->add('publishedAt', 'date', array(
    'input'  => 'datetime',
    'widget' => 'choice',
));

The input option must be changed to match the type of the underlying date data. For example, if the publishedAt field's data were a unix timestamp, you'd need to set input to timestamp:

$builder->add('publishedAt', 'date', array(
    'input'  => 'timestamp',
    'widget' => 'choice',
));

The field also supports an array and string as valid input option values.

Field Options

empty_value

type: string or array

If your widget option is set to choice, then this field will be represented as a series of select boxes. The empty_value option can be used to add a "blank" entry to the top of each select box:

$builder->add('dueDate', 'date', array(
    'empty_value' => '',
));

Alternatively, you can specify a string to be displayed for the "blank" value:

$builder->add('dueDate', 'date', array(
    'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day')
));

Overridden Options

by_reference

default: false

The DateTime classes are treated as immutable objects.

error_bubbling

default: false

Inherited options

These options inherit from the form </reference/forms/types/form> type:

Field Variables

Variable Type Usage
widget mixed The value of the widget option.
type string Only present when widget is single_text and HTML5 is activated, contains the input type to use (datetime, date or time).
date_pattern string A string with the date format to use.