Skip to content
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

Leading 0 in number form type with region code context #145

Open
bk-tb opened this issue Apr 15, 2023 · 1 comment
Open

Leading 0 in number form type with region code context #145

bk-tb opened this issue Apr 15, 2023 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@bk-tb
Copy link

bk-tb commented Apr 15, 2023

If the widget PhoneNumberType::WIDGET_COUNTRY_CHOICE is used, the number in the number form type contains leading zeros. In the context of the select form type of the region code this is unexpected: +49 02408 95660 instead of +49 2408 95660

Currently the PhoneNumberToArrayTransformer uses
'number' => $util->format($phoneNumber, PhoneNumberFormat::NATIONAL),

Perhaps the better way is to strip the leading zeros:

'number' => $this->getNationalNumberForRegionCodeContext($util->format($phoneNumber, PhoneNumberFormat::NATIONAL), $phoneNumber),

    private function getNationalNumberForRegionCodeContext(string $nationalNumber, PhoneNumber $phoneNumber): string
    {
        if (!$phoneNumber->hasItalianLeadingZero() && $phoneNumber->getNumberOfLeadingZeros() > 0 && str_starts_with($nationalNumber, str_repeat('0', $phoneNumber->getNumberOfLeadingZeros()))) {
            return substr($nationalNumber, $phoneNumber->getNumberOfLeadingZeros());
        }
        return $nationalNumber;
    }

Other numbers perhabs for unit tests:
+43533765123
+41625571123
+390669812123 (hasItalianLeadingZero)
+390863530123
+39393508123

@bk-tb bk-tb changed the title Leading 0 in number form field with region code context Leading 0 in number form type with region code context Apr 17, 2023
@ngrie
Copy link

ngrie commented Apr 17, 2023

For reference, there already was an issue for this in the original repository: misd-service-development#174

@maxhelias maxhelias added the bug Something isn't working label Apr 24, 2023
@maxhelias maxhelias added help wanted Extra attention is needed good first issue Good for newcomers labels Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants