diff --git a/UPGRADE-4.3.md b/UPGRADE-4.3.md index 189c995bbef3d..1e9f0de938b1e 100644 --- a/UPGRADE-4.3.md +++ b/UPGRADE-4.3.md @@ -23,6 +23,7 @@ Config Form ---- + * Support for using integers as form names is deprecated and will lead to a type error in 5.0, use strings instead. * 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..00376d7a5d4e7 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -78,6 +78,7 @@ Finder Form ---- + * Using integers as form names is not supported anymore, use strings instead. * 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/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index b6d83d613b01e..1c4f5dddba67a 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 4.3.0 ----- + * deprecated support for using integers as form names, use strings instead * 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/FormConfigBuilder.php b/src/Symfony/Component/Form/FormConfigBuilder.php index 344751b15976d..5297dfa5348de 100644 --- a/src/Symfony/Component/Form/FormConfigBuilder.php +++ b/src/Symfony/Component/Form/FormConfigBuilder.php @@ -179,6 +179,10 @@ public function __construct($name, ?string $dataClass, EventDispatcherInterface { self::validateName($name); + if (\is_int($name)) { + @trigger_error('Using integers as form names is deprecated in Symfony 4.3 and will lead to a type error in 5.0. Use strings instead.', E_USER_DEPRECATED); + } + if (null !== $dataClass && !class_exists($dataClass) && !interface_exists($dataClass, false)) { throw new InvalidArgumentException(sprintf('Class "%s" not found. Is the "data_class" form option set correctly?', $dataClass)); } diff --git a/src/Symfony/Component/Form/Tests/FormConfigTest.php b/src/Symfony/Component/Form/Tests/FormConfigTest.php index 18dac5528f97f..94267e365fb53 100644 --- a/src/Symfony/Component/Form/Tests/FormConfigTest.php +++ b/src/Symfony/Component/Form/Tests/FormConfigTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Form\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Form\FormConfigBuilder; /** @@ -52,9 +53,6 @@ public function getHtml4Ids() // For root forms, leading underscores will be stripped from the // "id" attribute to produce valid HTML4. ['_'], - // Integers are allowed - [0], - [123], // NULL is allowed [null], // Other types are not @@ -83,6 +81,26 @@ public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $expectedExcept $this->assertSame((string) $name, $formConfigBuilder->getName()); } + /** + * @group legacy + * @expectedDeprecation Using integers as form names is deprecated in Symfony 4.3 and will lead to a type error in 5.0. Use strings instead. + * @dataProvider getHtml4IntegerIds + */ + public function testNameContainingOnlyDigitsPassedAsInteger($name) + { + $formConfigBuilder = new FormConfigBuilder($name, null, new EventDispatcher()); + + $this->assertSame((string) $name, $formConfigBuilder->getName()); + } + + public function getHtml4IntegerIds() + { + return [ + [0], + [123], + ]; + } + public function testGetRequestHandlerCreatesNativeRequestHandlerIfNotSet() { $config = $this->getConfigBuilder()->getFormConfig();