Symfony 4.3 will include lots of new features related to forms, such as simpler form theming, more translation options and other improvements such as the option to include HTML contents in help messages.

This article shows more of those new features added in Symfony 4.3 to improve the development experience of forms.

Keep preferred choices order

Valentin Udaltsov
Contributed by Valentin Udaltsov in #30985

The ChoiceType form field used to display select drop-downs, checkboxes and radio buttons defines an option called preferred_choices to display some options at the top of your list with a visual separator between them and the rest of the options:

1
2
3
4
5
6
7
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Intl\Countries;

->add('country', ChoiceType::class, [
    'choices' => Countries::getNames(),
    'preferred_choices' => ['US', 'JP', 'CN'],
])

In previous Symfony versions, the preferred choices were sorted automatically as the rest of the options (in the previous example, they will be displayed in this order: CN, JP, US). In Symfony 4.3, these preferred options will be displayed in the exact same order as you defined in your code.

Improved the form debug command

Yonel Ceruto
Contributed by Yonel Ceruto in #31082

The handy debug:form command has improved in Symfony 4.3. If a form type defines options with OptionsResolver normalizers associated to them, the command now displays all of them:

Row attributes in form themes

Alexander Schranz
Contributed by Alexander Schranz in #30320

In current Symfony versions, when you need to do minor customizations (e.g. adding a CSS class) to the form_row block in form themes you need to override the entire form_row block contents. In Symfony 4.3, thanks to a new row_attr option, you can do that as follows:

1
2
3
4
{% block form_row %}
    {% set row_attr = { class: 'some_custom_class' } %}
    {{ parent() }}
{% endblock %}

Custom errors in data mappers

Maxime Steinhausser
Contributed by Maxime Steinhausser in #20978

Sometimes, when using data mappers with Symfony Forms, the generic error message shown when a data transformation fails is not clear enough. In Symfony 4.3 you'll be able to define a custom error message:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class MoneyDataMapper implements DataMapperInterface
{
    public function mapDataToForms($data, $forms)
    {
        $forms = iterator_to_array($forms);
        $forms['amount']->setData($data ? $data->getAmount() : 0);
        $forms['currency']->setData($data ? $data->getCurrency() : 'EUR');
    }

    public function mapFormsToData($forms, &$data)
    {
        $forms = iterator_to_array($forms);

        $amount = $forms['amount']->getData();
        if (!is_numeric($amount)) {
            $failure = new TransformationFailedException('Expected numeric value');

            // 'setInvalidMessage()' method was introduced in Symfony 4.3
            $failure->setInvalidMessage('Money amount should be numeric. {{ amount }} is invalid.', [
                '{{ amount }}' => json_encode($amount)
            ]);

            throw $failure;
        }

        $data = new Money($forms['amount']->getData(), $forms['currency']->getData());
    }
}

Customizable percent symbol

Oskar Stark
Contributed by Oskar Stark in #30433

By default, Symfony uses % as the symbol displayed next to the values of PercentType form fields. In Symfony 4.3 this is configurable thanks to a new option called symbol. Set it to false to not display any symbol or set it to a string value (e.g. ) to display any symbol.

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

->add('salinity', PercentType::class, [
    'symbol' => '‰',
])
Published in #Living on the edge