Skip to content

Commit 8f56315

Browse files
hiddewiexabbuh
authored andcommitted
Improve invalid messages for form types
1 parent 319544a commit 8f56315

20 files changed

+68
-8
lines changed

DependencyInjection/Configuration.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
189189
->scalarNode('field_name')->defaultValue('_token')->end()
190190
->end()
191191
->end()
192+
// to be set to false in Symfony 6.0
193+
->booleanNode('legacy_error_messages')
194+
->defaultTrue()
195+
->validate()
196+
->ifTrue()
197+
->then(function ($v) {
198+
@trigger_error('Since symfony/framework-bundle 5.2: Setting the "framework.form.legacy_error_messages" option to "true" is deprecated. It will have no effect as of Symfony 6.0.', E_USER_DEPRECATED);
199+
200+
return $v;
201+
})
202+
->end()
203+
->end()
192204
->end()
193205
->end()
194206
->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,8 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont
514514
{
515515
$loader->load('form.php');
516516

517+
$container->getDefinition('form.type_extension.form.validator')->setArgument(1, $config['form']['legacy_error_messages']);
518+
517519
if (null === $config['form']['csrf_protection']['enabled']) {
518520
$config['form']['csrf_protection']['enabled'] = $config['csrf_protection']['enabled'];
519521
}

Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<xsd:element name="csrf-protection" type="form_csrf_protection" minOccurs="0" maxOccurs="1" />
5353
</xsd:all>
5454
<xsd:attribute name="enabled" type="xsd:boolean" />
55+
<xsd:attribute name="legacy-error-messages" type="xsd:boolean" />
5556
</xsd:complexType>
5657

5758
<xsd:complexType name="form_csrf_protection">

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ protected static function getBundleDefaultConfig()
354354
'enabled' => null, // defaults to csrf_protection.enabled
355355
'field_name' => '_token',
356356
],
357+
'legacy_error_messages' => true,
357358
],
358359
'esi' => ['enabled' => false],
359360
'ssi' => ['enabled' => false],

Tests/DependencyInjection/Fixtures/php/csrf.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
$container->loadFromExtension('framework', [
44
'csrf_protection' => true,
5-
'form' => true,
5+
'form' => [
6+
'legacy_error_messages' => false,
7+
],
68
'session' => [
79
'handler_id' => null,
810
],
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'form' => [
5+
'legacy_error_messages' => true,
6+
],
7+
]);

Tests/DependencyInjection/Fixtures/php/form_no_csrf.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
'csrf_protection' => [
66
'enabled' => false,
77
],
8+
'legacy_error_messages' => false,
89
],
910
]);

Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'csrf_protection' => [
99
'field_name' => '_csrf',
1010
],
11+
'legacy_error_messages' => false,
1112
],
1213
'http_method_override' => false,
1314
'esi' => [

Tests/DependencyInjection/Fixtures/xml/csrf.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<framework:config>
1010
<framework:csrf-protection />
11-
<framework:form />
11+
<framework:form legacy-error-messages="false" />
1212
<framework:session />
1313
</framework:config>
1414
</container>

Tests/DependencyInjection/Fixtures/xml/form_csrf_sets_field_name.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<framework:config>
1010
<framework:csrf-protection field-name="_custom" />
1111
<framework:session />
12-
<framework:form />
12+
<framework:form legacy-error-messages="false" />
1313
</framework:config>
1414
</container>

0 commit comments

Comments
 (0)