Disabling Validation when Submitting Forms
The formnovalidate HTML attribute (defined only for image
and
input[type=submit]
elements) allows to disable client-side validation when
submitting a form.
In Symfony 4.4, submit buttons (SubmitType
) define a new validate
boolean option to enable/disable this validation:
1 2 3 4 5 6 7
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
// ...
$builder->add('save', SubmitType::class);
$builder->add('save-as-draft', SubmitType::class, [
'validate' => false,
]);
Added Support for Alpha-3 Codes
The CountryType form field uses Alpha-2 codes by default to refer to each
country (e.g. bw
= Botswana, sg
= Singapore, etc.) In Symfony 4.4, we
added a new alpha3
option to it so you can use ISO 3166-1 alpha-3 codes
too (e.g. bwa
= Botswana, sgp
= Singapore):
1 2 3 4 5 6
use Symfony\Component\Form\Extension\Core\Type\CountryType;
// ...
$builder->add('country', CountryType::class, [
'alpha3' => true,
]);
Related to this, in the Pull Request #32988, Terje Bråten added support for Alpha-3 codes in the Intl component:
1 2 3 4 5 6 7 8 9 10
use Symfony\Component\Intl\Languages;
use Symfony\Component\Intl\Countries;
$languages = Languages::getAlpha3Names();
$isValidLanguage = Languages::alpha3CodeExists($alpha3Code);
$alpha3Code = Languages::getAlpha3Code($alpha2Code);
$countries = Countries::getAlpha3Names();
$isValidCountry = Countries::alpha3CodeExists($alpha3Code);
$alpha3Code = Countries::getAlpha3Code($alpha2Code);
Show Preferred Choices Twice
The preferred_choices option of the ChoiceType form field allows to display some choices at the top of the list (e.g. to display first the most popular shipping countries in your store).
Although this option is handy for most users, others are confused because the preferred choices are no longer displayed in the full list of choices. That's why starting from Symfony 4.4, preferred choices are displayed twice: at the top of the choice list and at their original location if they weren't preferred.
Automatic Accept Attribute
In Symfony 4.4, when you define the mimeTypes option in a File constraint
applied to a FileType form field, the value of the mimeTypes
option is
also used in the accept
attribute of the related <input type="file"/>
HTML element.
This behavior is applied only when using form type guessing and when the field
doesn't define its own accept
value.
Getting the Form Name in Tests
When testing forms in functional tests, it's common to use some code like the
following, which hardcodes the form name (my_form[...]
in this example):
1 2 3 4 5 6 7 8
$client = static::createClient();
$crawler = $client->request('GET', '/sign-up');
$formButton = $crawler->selectButton('submit');
$form = $formButton->form([
'my_form[name]' => '...',
'my_form[password]' => '...',
// ...
]);
In Symfony 4.4, we added a getName()
method so you can get the form name
instead of hardcoding it:
1 2 3 4 5 6 7
// ...
$form = $formButton->form();
$formName = $form->getName();
$client->submit($form, [
$formName.'[name]' => '...',
$formName.'[password]' => '...',
]);
Awesome! Thanks
Please rewrite the last example, as
$form
is undefined before calling$form = $formButton
.And as
$formName
is used inside$formButton
its not possible.@Martin you are right! I've just updated the example. Thanks!
@Javier, $form->getName() went somewhere.
Fixed the code again. I'm sorry!
Thanks for the effort on keeping the docs up to date. Cheers!
Thank you, guys. Good work! :)