Skip to content

[Form] Improve invalid messages for form types #30931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
->scalarNode('field_name')->defaultValue('_token')->end()
->end()
->end()
// to be set to false in Symfony 6.0
->booleanNode('legacy_error_messages')
->defaultTrue()
->validate()
->ifTrue()
->then(function ($v) {
@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);

return $v;
})
->end()
->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont
{
$loader->load('form.php');

$container->getDefinition('form.type_extension.form.validator')->setArgument(1, $config['form']['legacy_error_messages']);

if (null === $config['form']['csrf_protection']['enabled']) {
$config['form']['csrf_protection']['enabled'] = $config['csrf_protection']['enabled'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<xsd:element name="csrf-protection" type="form_csrf_protection" minOccurs="0" maxOccurs="1" />
</xsd:all>
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="legacy-error-messages" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="form_csrf_protection">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ protected static function getBundleDefaultConfig()
'enabled' => null, // defaults to csrf_protection.enabled
'field_name' => '_token',
],
'legacy_error_messages' => true,
],
'esi' => ['enabled' => false],
'ssi' => ['enabled' => false],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

$container->loadFromExtension('framework', [
'csrf_protection' => true,
'form' => true,
'form' => [
'legacy_error_messages' => false,
],
'session' => [
'handler_id' => null,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$container->loadFromExtension('framework', [
'form' => [
'legacy_error_messages' => true,
],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
'csrf_protection' => [
'enabled' => false,
],
'legacy_error_messages' => false,
],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'csrf_protection' => [
'field_name' => '_csrf',
],
'legacy_error_messages' => false,
],
'http_method_override' => false,
'esi' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<framework:config>
<framework:csrf-protection />
<framework:form />
<framework:form legacy-error-messages="false" />
<framework:session />
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<framework:config>
<framework:csrf-protection field-name="_custom" />
<framework:session />
<framework:form />
<framework:form legacy-error-messages="false" />
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<framework:config>
<framework:csrf-protection field-name="_custom_form" />
<framework:form />
<framework:form legacy-error-messages="false" />
<framework:session />
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:form legacy-error-messages="true" />
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:form enabled="true">
<framework:form enabled="true" legacy-error-messages="false">
<framework:csrf-protection enabled="false" />
</framework:form>
</framework:config>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<framework:config secret="s3cr3t" ide="file%%link%%format" default-locale="fr" http-method-override="false">
<framework:csrf-protection />
<framework:form>
<framework:form legacy-error-messages="false">
<framework:csrf-protection field-name="_csrf"/>
</framework:form>
<framework:esi enabled="true" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
framework:
secret: s3cr3t
csrf_protection: ~
form: ~
form:
legacy_error_messages: false
session: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
framework:
form:
legacy_error_messages: true
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ framework:
form:
csrf_protection:
enabled: false
legacy_error_messages: false
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ framework:
form:
csrf_protection:
field_name: _csrf
legacy_error_messages: false
http_method_override: false
esi:
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Annotations\Annotation;
use Psr\Log\LoggerAwareInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyMessage;
Expand Down Expand Up @@ -57,6 +58,8 @@

abstract class FrameworkExtensionTest extends TestCase
{
use ExpectDeprecationTrait;

private static $containerCache = [];

abstract protected function loadFromFile(ContainerBuilder $container, $file);
Expand Down Expand Up @@ -1015,6 +1018,16 @@ public function testFormsCanBeEnabledWithoutCsrfProtection()
$this->assertFalse($container->getParameter('form.type_extension.csrf.enabled'));
}

/**
* @group legacy
*/
public function testFormsWithoutImprovedValidationMessages()
{
$this->expectDeprecation('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.');

$this->createContainerFromFile('form_legacy_messages');
}

public function testStopwatchEnabledWithDebugModeEnabled()
{
$container = $this->createContainerFromFile('default_config', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ framework:
router: { resource: "%kernel.project_dir%/%kernel.test_case%/routing.yml", utf8: true }
validation: { enabled: true, enable_annotations: true }
csrf_protection: true
form: true
form:
enabled: true
legacy_error_messages: false
test: true
default_locale: en
session:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ framework:
router: { resource: "%kernel.project_dir%/%kernel.test_case%/routing.yml", utf8: true }
validation: { enabled: true, enable_annotations: true }
csrf_protection: true
form: true
form:
enabled: true
legacy_error_messages: false
test: ~
default_locale: en
session:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ framework:
validation: { enabled: true, enable_annotations: true }
assets: ~
csrf_protection: true
form: true
form:
enabled: true
legacy_error_messages: false
test: ~
default_locale: en
session:
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/SecurityBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"symfony/dom-crawler": "^4.4|^5.0",
"symfony/expression-language": "^4.4|^5.0",
"symfony/form": "^4.4|^5.0",
"symfony/framework-bundle": "^4.4|^5.0",
"symfony/framework-bundle": "^5.2",
"symfony/process": "^4.4|^5.0",
"symfony/serializer": "^4.4|^5.0",
"symfony/translation": "^4.4|^5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ public function convertTransformationFailureToFormError(FormEvent $event)
}

$clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : get_debug_type($form->getViewData());
$messageTemplate = 'The value {{ value }} is not valid.';
$messageTemplate = $form->getConfig()->getOption('invalid_message', 'The value {{ value }} is not valid.');
$messageParameters = array_replace(['{{ value }}' => $clientDataAsString], $form->getConfig()->getOption('invalid_message_parameters', []));

if (null !== $this->translator) {
$message = $this->translator->trans($messageTemplate, ['{{ value }}' => $clientDataAsString]);
$message = $this->translator->trans($messageTemplate, $messageParameters);
} else {
$message = strtr($messageTemplate, ['{{ value }}' => $clientDataAsString]);
$message = strtr($messageTemplate, $messageParameters);
}

$form->addError(new FormError($message, $messageTemplate, ['{{ value }}' => $clientDataAsString], null, $form->getTransformationFailure()));
$form->addError(new FormError($message, $messageTemplate, $messageParameters, null, $form->getTransformationFailure()));
}
}
10 changes: 9 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BirthdayType extends AbstractType
Expand All @@ -21,7 +22,14 @@ class BirthdayType extends AbstractType
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('years', range((int) date('Y') - 120, date('Y')));
$resolver->setDefaults([
'years' => range((int) date('Y') - 120, date('Y')),
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please enter a valid birthdate.';
},
]);

$resolver->setAllowedTypes('years', 'array');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CheckboxType extends AbstractType
Expand Down Expand Up @@ -60,6 +61,11 @@ public function configureOptions(OptionsResolver $resolver)
'empty_data' => $emptyData,
'compound' => false,
'false_values' => [null],
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'The checkbox has an invalid value.';
},
'is_empty_callback' => static function ($modelData): bool {
return false === $modelData;
},
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ public function configureOptions(OptionsResolver $resolver)
'data_class' => null,
'choice_translation_domain' => true,
'trim' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'The selected choice is invalid.';
},
]);

$resolver->setNormalizer('placeholder', $placeholderNormalizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public function configureOptions(OptionsResolver $resolver)
'entry_type' => TextType::class,
'entry_options' => [],
'delete_empty' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'The collection is invalid.';
},
]);

$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/ColorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand Down Expand Up @@ -69,6 +70,11 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'html5' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please select a valid color.';
},
]);

$resolver->setAllowedTypes('html5', 'bool');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function configureOptions(OptionsResolver $resolver)
'choice_translation_domain' => false,
'choice_translation_locale' => null,
'alpha3' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please select a valid country.';
},
]);

$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function configureOptions(OptionsResolver $resolver)
},
'choice_translation_domain' => false,
'choice_translation_locale' => null,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please select a valid currency.';
},
]);

$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ public function configureOptions(OptionsResolver $resolver)
'compound' => $compound,
'empty_data' => $emptyData,
'labels' => [],
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please choose a valid date interval.';
},
]);
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
$resolver->setNormalizer('labels', $labelsNormalizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ public function configureOptions(OptionsResolver $resolver)
return $options['compound'] ? [] : '';
},
'input_format' => 'Y-m-d H:i:s',
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please enter a valid date and time.';
},
]);

// Don't add some defaults in order to preserve the defaults
Expand Down
Loading