From 405aa548eb8780c190bba914b7d0fa0cca078884 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 24 Oct 2018 15:24:37 +0200 Subject: [PATCH] deprecate using invalid names for buttons --- UPGRADE-4.3.md | 4 ++++ UPGRADE-5.0.md | 5 ++++- src/Symfony/Component/Form/ButtonBuilder.php | 12 ++++++++++++ src/Symfony/Component/Form/CHANGELOG.md | 4 ++++ .../Component/Form/Tests/ButtonBuilderTest.php | 9 ++++++++- 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/UPGRADE-4.3.md b/UPGRADE-4.3.md index 189c995bbef3d..e867041dc2669 100644 --- a/UPGRADE-4.3.md +++ b/UPGRADE-4.3.md @@ -23,6 +23,10 @@ Config Form ---- + * Using names for buttons that do not start with a letter, a digit, or an underscore is deprecated and will lead to an + exception in 5.0. + * Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons is deprecated and + will lead to an exception in 5.0. * Using the `date_format`, `date_widget`, and `time_widget` options of the `DateTimeType` when the `widget` option is set to `single_text` is deprecated. diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index 62aae8234a2ef..5bfc0da172d19 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -77,7 +77,10 @@ Finder Form ---- - + + * Using names for buttons that do not start with a letter, a digit, or an underscore leads to an exception. + * Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons leads to an + exception. * Using the `date_format`, `date_widget`, and `time_widget` options of the `DateTimeType` when the `widget` option is set to `single_text` is not supported anymore. * The `getExtendedType()` method was removed from the `FormTypeExtensionInterface`. It is replaced by the the static diff --git a/src/Symfony/Component/Form/ButtonBuilder.php b/src/Symfony/Component/Form/ButtonBuilder.php index 5a67b7d23f872..38377db30fa04 100644 --- a/src/Symfony/Component/Form/ButtonBuilder.php +++ b/src/Symfony/Component/Form/ButtonBuilder.php @@ -63,6 +63,18 @@ public function __construct(?string $name, array $options = []) $this->name = $name; $this->options = $options; + + if (\preg_match('/^([^a-z0-9_].*)?(.*[^a-zA-Z0-9_\-:].*)?$/D', $name, $matches)) { + if (isset($matches[1])) { + @trigger_error(sprintf('Using names for buttons that do not start with a letter, a digit, or an underscore is deprecated since Symfony 4.3 and will throw an exception in 5.0 ("%s" given).', $name), E_USER_DEPRECATED); + } + if (isset($matches[2])) { + @trigger_error(sprintf('Using names for buttons that do not contain only letters, digits, underscores ("_"), hyphens ("-") and colons (":") ("%s" given) is deprecated since Symfony 4.3 and will throw an exception in 5.0.', $name), E_USER_DEPRECATED); + } + } + + // to be added in 5.0 + // FormConfigBuilder::validateName($name); } /** diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index b6d83d613b01e..4b54aa63dfe49 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -4,6 +4,10 @@ CHANGELOG 4.3.0 ----- + * Using names for buttons that do not start with a letter, a digit, or an underscore is deprecated and will lead to an + exception in 5.0. + * Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons is deprecated and + will lead to an exception in 5.0. * deprecated using the `date_format`, `date_widget`, and `time_widget` options of the `DateTimeType` when the `widget` option is set to `single_text` * added `block_prefix` option to `BaseType`. diff --git a/src/Symfony/Component/Form/Tests/ButtonBuilderTest.php b/src/Symfony/Component/Form/Tests/ButtonBuilderTest.php index ced54a4588b59..e987ef7eabf20 100644 --- a/src/Symfony/Component/Form/Tests/ButtonBuilderTest.php +++ b/src/Symfony/Component/Form/Tests/ButtonBuilderTest.php @@ -28,7 +28,6 @@ public function getValidNames() ['foo'], ['0'], [0], - ['button[]'], ]; } @@ -40,6 +39,14 @@ public function testValidNames($name) $this->assertInstanceOf('\Symfony\Component\Form\ButtonBuilder', new ButtonBuilder($name)); } + /** + * @group legacy + */ + public function testNameContainingIllegalCharacters() + { + $this->assertInstanceOf('\Symfony\Component\Form\ButtonBuilder', new ButtonBuilder('button[]')); + } + public function getInvalidNames() { return [