New in Symfony 4.3: More Form improvements
May 10, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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
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
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
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
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
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' => '‰',
])
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.