Symfony News

New in Symfony 4.1: Added support for immutable dates in forms

Valentin Udaltsov

Contributed by
Valentin Udaltsov
in #25582.

The DateTimeImmutable class was introduced in PHP 5.5. It provides the same API as DateTime, but when calling to its modify() or set*() methods, it returns a new DateTimeImmutable object to not change the original value.

In recent Symfony versions we added support for immutable dates in several parts of the framework. In Symfony 4.1 we finished a long-standing petition from the community to add support for immutable dates in Symfony Forms.

When building a form with a DateTimeType, DateType or TimeType field, set their input options to the new datetime_immutable value:

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

$builder->add('startsAt', DateTimeType::class, array(
    'input' => 'datetime_immutable',
));

Now you can set a DateTimeImmutable object as the value of this field and when the form is submitted and valid, you'll get a DateTimeImmutable object back with the new value set by the user.


Be trained by Symfony experts - 2018-02-7 Paris - 2018-02-12 Berlin - 2018-02-12 Berlin


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook