New in Symfony 3.2: DateInterval form type

Contributed by
Steffen Roßkamp
in #16809.

Symfony includes 32 built-in form types that cover a wide range of needs for enterprise applications. Our community is continuously proposing new form types that we include when they solve common enough needs. That's why in Symfony 3.2 we decided to include a new DateInterval form type.

This form type is useful for applications dealing with reminders, bookings and similar information. For example, if your model defines a remindEvery property that stores a DateInterval PHP object, add the following to edit its value using three <select> elements:

1
2
3
4
5
6
use Symfony\Component\Form\Extension\Core\Type\DateIntervalType;

// remindEvery is a DateInterval PHP object
$builder->add('remindEvery', DateIntervalType::class, array(
    'widget' => 'choice',
));

The DateInterval type is very flexible and it can manipulate DateInterval PHP objects, arrays and ISO 8601 duration strings. When using the ISO 8601 format, set the input option to string to allow the form type transform the value properly:

1
2
3
4
5
// remindEvery is a PHP string using ISO 8601 format
$builder->add('remindEvery', DateIntervalType::class, array(
    'input' => 'string',
    'widget' => 'choice',
));

Besides input and widget this form type defines 18 configuration options to control whether years, weeks, months, days, hours, minutes and seconds are displayed, their placeholders and the values allowed to be selected.

The documentation for this form type hasn't been merged yet, but you can already read it in this Symfony Docs pull request.

Comments

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.