Skip to content

[Form] Fix handling the empty string in NumberToLocalizedStringTransformer #60676

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

Conversation

gnat42
Copy link
Contributor

@gnat42 gnat42 commented Jun 3, 2025

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues
License MIT

The NumberToLocalizedStringTransformer only tests for null, but if the value is an empty string then the test for is_numeric fails. This change makes the reverseTransform and transform methods both test for null or empty strings

@carsonbot carsonbot added this to the 6.4 milestone Jun 3, 2025
@gnat42 gnat42 force-pushed the gnat/number-to-localized-empty-value branch from d310abb to 9bf812e Compare June 3, 2025 20:29
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

I can't tell about the change itself, but here is a CS issue :)

@OskarStark OskarStark added the Form label Jun 4, 2025
@carsonbot carsonbot changed the title Gnat/number to localized empty value [Form] Gnat/number to localized empty value Jun 4, 2025
@gnat42 gnat42 force-pushed the gnat/number-to-localized-empty-value branch from 9bf812e to eec5f2e Compare June 4, 2025 14:51
@gnat42
Copy link
Contributor Author

gnat42 commented Jun 4, 2025

As an aside this is basically doing the other half of #37505

Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

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

Not sure why we didn’t reject numeric strings before as the transformer is expected to work with ints and floats, but as things are this way the change looks valid to me.

@OskarStark OskarStark changed the title [Form] Gnat/number to localized empty value [Form] number to localized empty value Jun 4, 2025
@@ -50,7 +50,7 @@ public function __construct(?int $scale = null, ?bool $grouping = false, ?int $r
*/
public function transform(mixed $value): string
{
if (null === $value) {
if (null === $value || '' === $value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn’t $value PHPdoc be updated to include string?

@nicolas-grekas
Copy link
Member

Don't we need the same check in MoneyToLocalizedStringTransformer?

@gnat42 gnat42 force-pushed the gnat/number-to-localized-empty-value branch from eec5f2e to f623d3a Compare June 5, 2025 16:22
@gnat42
Copy link
Contributor Author

gnat42 commented Jun 5, 2025

I've pushed another version that includes changes to the docblock and identical changes to MoneyToLocalizedStringTransformer including a test (I didn't run them locally so will push an update if somehow I did it wrong).

@nicolas-grekas nicolas-grekas changed the title [Form] number to localized empty value [Form] Fix handling the empty string in NumberToLocalizedStringTransformer Jun 6, 2025
@nicolas-grekas
Copy link
Member

Thank you @gnat42.

@nicolas-grekas nicolas-grekas merged commit 09d02eb into symfony:6.4 Jun 6, 2025
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants