Skip to content

Commit 6a458bd

Browse files
committed
add validation to Street, city, and phone number fields
1 parent 414a346 commit 6a458bd

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\Validator;
9+
10+
use Magento\Customer\Model\Customer;
11+
use Magento\Framework\Validator\AbstractValidator;
12+
13+
/**
14+
* Customer city fields validator.
15+
*/
16+
class City extends AbstractValidator
17+
{
18+
/**
19+
* Allowed characters:
20+
*
21+
* \p{L}: Unicode letters.
22+
* \p{M}: Unicode marks (diacritic marks, accents, etc.).
23+
* ,: Comma.
24+
* \-: Hyphen.
25+
* \.: Period.
26+
* `'’: Single quotes, both regular and right single quotation marks.
27+
* &: Ampersand.
28+
* \s: Whitespace characters (spaces, tabs, newlines, etc.).
29+
* \d: Digits (0-9).
30+
*/
31+
private const PATTERN_CITY = '/(?:[\p{L}\p{M}\,\-\.\'’`&\s\d]){1,255}+/u';
32+
33+
/**
34+
* Validate city fields.
35+
*
36+
* @param Customer $customer
37+
* @return bool
38+
*/
39+
public function isValid($customer)
40+
{
41+
if (!$this->isValidCity($customer->getCity())) {
42+
parent::_addMessages([[
43+
'city' => 'City is not valid! Allowed chars: Unicode letters, Unicode marks, Comma, Hyphen, Period, Single quotes, both regular and right single quotation marks, Ampersand, Whitespace characters, Digits'
44+
]]);
45+
}
46+
47+
return count($this->_messages) == 0;
48+
}
49+
50+
/**
51+
* Check if city field is valid.
52+
*
53+
* @param string|null $cityValue
54+
* @return bool
55+
*/
56+
private function isValidCity($cityValue)
57+
{
58+
if ($cityValue != null) {
59+
if (preg_match(self::PATTERN_CITY, $cityValue, $matches)) {
60+
return $matches[0] == $cityValue;
61+
}
62+
}
63+
64+
return true;
65+
}
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\Validator;
9+
10+
use Magento\Customer\Model\Customer;
11+
use Magento\Framework\Validator\AbstractValidator;
12+
13+
/**
14+
* Customer street fields validator.
15+
*/
16+
class Street extends AbstractValidator
17+
{
18+
/**
19+
* Allowed characters:
20+
*
21+
* \p{L}: Unicode letters.
22+
* \p{M}: Unicode marks (diacritic marks, accents, etc.).
23+
* ,: Comma.
24+
* \-: Hyphen.
25+
* \.: Period.
26+
* `'’: Single quotes, both regular and right single quotation marks.
27+
* &: Ampersand.
28+
* \s: Whitespace characters (spaces, tabs, newlines, etc.).
29+
* \d: Digits (0-9).
30+
*/
31+
private const PATTERN_STREET = '/(?:[\p{L}\p{M}\,\-\.\'’`&\s\d]){1,255}+/u';
32+
33+
/**
34+
* Validate street fields.
35+
*
36+
* @param Customer $customer
37+
* @return bool
38+
*/
39+
public function isValid($customer)
40+
{
41+
foreach ($customer->getStreet() as $street) {
42+
if (!$this->isValidStreet($street)) {
43+
parent::_addMessages([['street' => 'Street is not valid! Allowed chars: Unicode letters, Unicode marks, Comma, Hyphen, Period, Single quotes, both regular and right single quotation marks, Ampersand, Whitespace characters, Digits']]);
44+
}
45+
}
46+
47+
return count($this->_messages) == 0;
48+
}
49+
50+
/**
51+
* Check if street field is valid.
52+
*
53+
* @param string|null $streetValue
54+
* @return bool
55+
*/
56+
private function isValidStreet($streetValue)
57+
{
58+
if ($streetValue != null) {
59+
if (preg_match(self::PATTERN_STREET, $streetValue, $matches)) {
60+
return $matches[0] == $streetValue;
61+
}
62+
}
63+
64+
return true;
65+
}
66+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\Validator;
9+
10+
use Magento\Customer\Model\Customer;
11+
use Magento\Framework\Validator\AbstractValidator;
12+
13+
/**
14+
* Customer telephone fields validator.
15+
*/
16+
class Telephone extends AbstractValidator
17+
{
18+
/**
19+
* Allowed char:
20+
*
21+
* \(\) :Matches open and close parentheses
22+
* \s: Matches any whitespace character.
23+
* \+: Matches the plus sign.
24+
* \-: Matches the hyphen.
25+
* \d: Digits (0-9).
26+
*/
27+
private const PATTERN_TELEPHONE = '/(?:[\(\)\+\-\s\d]){1,255}+/u';
28+
29+
/**
30+
* Validate telephone fields.
31+
*
32+
* @param Customer $customer
33+
* @return bool
34+
*/
35+
public function isValid($customer)
36+
{
37+
if (!$this->isValidTelephone($customer->getTelephone())) {
38+
parent::_addMessages([['telephone' => 'Telephone is not valid! Allowed chars: Matches open and close parentheses, Matches any whitespace character, Matches the plus sign, Matches the hyphen, Digits (0-9)']]);
39+
}
40+
41+
return count($this->_messages) == 0;
42+
}
43+
44+
/**
45+
* Check if telephone field is valid.
46+
*
47+
* @param string|null $telephoneValue
48+
* @return bool
49+
*/
50+
private function isValidTelephone($telephoneValue)
51+
{
52+
if ($telephoneValue != null) {
53+
if (preg_match(self::PATTERN_TELEPHONE, $telephoneValue, $matches)) {
54+
return $matches[0] == $telephoneValue;
55+
}
56+
}
57+
58+
return true;
59+
}
60+
}

app/code/Magento/Customer/etc/validation.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,30 @@
5151
<constraint alias="name_validator" class="Magento\Customer\Model\Validator\Name" />
5252
</entity_constraints>
5353
</rule>
54+
<rule name="check_street">
55+
<entity_constraints>
56+
<constraint alias="street_validator" class="Magento\Customer\Model\Validator\Street" />
57+
</entity_constraints>
58+
</rule>
59+
<rule name="check_city">
60+
<entity_constraints>
61+
<constraint alias="city_validator" class="Magento\Customer\Model\Validator\City" />
62+
</entity_constraints>
63+
</rule>
64+
<rule name="check_telephone">
65+
<entity_constraints>
66+
<constraint alias="telephone_validator" class="Magento\Customer\Model\Validator\Telephone" />
67+
</entity_constraints>
68+
</rule>
5469
</rules>
5570
<groups>
5671
<group name="save">
5772
<uses>
5873
<use rule="check_eav"/>
5974
<use rule="check_name"/>
75+
<use rule="check_street"/>
76+
<use rule="check_city"/>
77+
<use rule="check_telephone"/>
6078
</uses>
6179
</group>
6280
</groups>

0 commit comments

Comments
 (0)