New in Symfony 5.1: Form improvements

Added html5 option to ColorType

Contributed by
Thomas Calvet
in #36302.

We added a new html5 option to ColorType form field. When this option is set to true, the form type checks that its value matches the HTML5 color format, which is /^#[0-9a-f]{6}$/i.

Added rounding_mode option to PercentType

Contributed by
Vincent Langlet
in #35729.

The new rounding_mode option of the PercentType form field is useful in combination with the scale option, which defines the number of decimals allowed before applying the rounding.

The values of rounding_mode are any of the PHP \NumberFormatter constants (\NumberFormatter::ROUND_CEILING, \NumberFormatter::ROUND_HALFEVEN, etc.)

Allow HTML contents in form labels

Contributed by
Przemysław Bogusz
in #31375.

HTML contents are escaped by default in form labels for security reasons. The new label_html boolean option allows a form field to include HTML contents in their labels, which is useful to display icons inside buttons, links and some formatting in checkbox/radiobutton labels, etc.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// src/Form/Type/TaskType.php
namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // ...
            ->add('save', SubmitType::class, [
                'label' => '<i class="far fa-save"></i> Save',
                'label_html' => true,
            ])
        ;
    }
}

Simpler reference_date in TimeType

Contributed by
Christian Flothmann
in #35205.

In the TimeType form field, when you use different values for model_timezone and view_timezone, you must set the reference_date option. In Symfony 5.1, when no reference_date is set, the view_timezone defaults to the configured model_timezone.

Better default values for the inputmode option

Contributed by
Artem Henvald
in #34986.

The inputmode HTML attribute tells browsers which kind of data might be entered by the user while editing the element or its contents (e.g. a telephone number, an email address, a decimal number, etc.)

We already use this option in form fields like UrlType, but in Symfony 5.1 we decided to configure better default values for the inputmode in several fields:

  • inputmode = 'email' for EmailType;
  • inputmode = 'search' for SearchType;
  • inputmode = 'tel' for TelType.

Choice improvements

Contributed by
Jules Pietri
in #35733.

The new choice_filter option allows you to filter the default list of choices configured for a given form field. Use a PHP closure to decide if a choice should be kept or removed (and combine it with custom form type options for maximum flexibility):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// src/Form/Type/AddressType.php
namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\FormBuilderInterface;

class AddressType extends AbstractType
{
    // ...

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // ...
            ->add('country', CountryType::class, [
                // $allowedCountries is a custom form type option
                // closure returns TRUE to keep the choice and FALSE to remove it
                'choice_filter' => $allowedCountries ? function ($countryCode) use ($allowedCountries) {
                    return in_array($countryCode, $allowedCountries, true);
                } : null,
            ])
        ;
    }

In addition, we updated the Symfony Forms internals to support caching choice lists based on options (see pull request #30994), providing between a 15% and 30% performance improvement.

Finally, we introduced an AbstractChoiceLoader to simplify the choice lazy-loading implementations and handle global optimizations (see pull request #34550).

Comments

Awesome, waited for the HTML labels so long. Perfect if you have a checkbox with a link to the Terms or similar.
Very nice! Thank you!
Thanks a lot for these form improvements ! 👍🏼

Comments are closed.

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