Skip to content

39854: Fix city name validation to allow digits, &, ., () characters #39859

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

Open
wants to merge 4 commits into
base: 2.4-develop
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions app/code/Magento/Customer/Model/Validator/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ class City extends AbstractValidator
*
* \p{L}: Unicode letters.
* \p{M}: Unicode marks (diacritic marks, accents, etc.).
* ': Apostrophe mark.
* \d: Digits (0-9).
* \s: Whitespace characters (spaces, tabs, newlines, etc.).
* -: Hyphen.
* _: Underscore.
* ', ’: Apostrophes (straight and typographical).
* .: Period/full stop.
* ,: Comma.
* &: Ampersand.
* (): Parentheses.
*/
private const PATTERN_CITY = '/(?:[\p{L}\p{M}\s\-\']{1,100})/u';
private const PATTERN_CITY = '/^[\p{L}\p{M}\d\s\-_\'’\.,&\(\)]{1,100}$/u';

/**
* Validate city fields.
Expand All @@ -35,7 +42,7 @@ public function isValid($customer)
{
if (!$this->isValidCity($customer->getCity())) {
parent::_addMessages([[
'city' => "Invalid City. Please use A-Z, a-z, 0-9, -, ', spaces"
'city' => "Invalid City. Please use letters, numbers, spaces, and the following characters: - _ ' ’ . , & ( )"
]]);
}

Expand Down
32 changes: 32 additions & 0 deletions app/code/Magento/Customer/Test/Unit/Model/Validator/CityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@ public static function expectedPunctuationInNamesDataProvider(): array
[
'city' => ' Moscow Moscow',
'message' => 'Whitespace characters must be allowed in city'
],
[
'city' => 'O\'Higgins',
'message' => 'Straight apostrophe must be allowed in city names'
],
[
'city' => 'O’Higgins',
'message' => 'Typographical apostrophe must be allowed in city names'
],
[
'city' => 'Saint_Petersburg',
'message' => 'Underscore must be allowed in city names'
],
[
'city' => 'Stratford-upon-Avon',
'message' => 'Hyphens must be allowed in city names'
],
[
'city' => 'St. Petersburg',
'message' => 'Periods must be allowed in city names'
],
[
'city' => 'Trinidad & Tobago',
'message' => 'Ampersand must be allowed in city names'
],
[
'city' => 'Winston-Salem (NC)',
'message' => 'Parentheses must be allowed in city names'
],
[
'city' => 'Rostov-on-Don, Russia',
'message' => 'Commas must be allowed in city names'
]
];
}
Expand Down