Skip to content

[Form] deprecate integers as form names #30032

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions UPGRADE-4.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will it? automatic casting rules would turn the int to a string and all will be silent, no?

* Using the `date_format`, `date_widget`, and `time_widget` options of the `DateTimeType` when the `widget` option is
set to `single_text` is deprecated.

Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/FormConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ public function __construct($name, ?string $dataClass, EventDispatcherInterface
{
self::validateName($name);

if (\is_int($name)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason why strings with numerical values are still allowed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CollectionType for example handles forms that have numerical names. And from an HTML point of view it does not really matter that the name does only contain digits.

@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));
}
Expand Down
24 changes: 21 additions & 3 deletions src/Symfony/Component/Form/Tests/FormConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\FormConfigBuilder;

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down