Skip to content

Commit de1bc17

Browse files
author
Mohan Ahuja
committed
ACP2E-1001: Customer Zip Code input validation rule not working
- Fixed input validation - Fixed multiple same type of error messages not showing on the UI.
1 parent cd826aa commit de1bc17

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Address/Validate.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ private function validateCustomerAddress(DataObject $response): DataObject
116116
if ($errors !== true) {
117117
$messages = $response->hasMessages() ? $response->getMessages() : [];
118118
foreach ($errors as $error) {
119-
$messages[] = $error;
119+
foreach ($error as $message) {
120+
$messages[] = $message;
121+
}
120122
}
121123
$response->setMessages($messages);
122124
$response->setError(true);

app/code/Magento/Customer/Model/Metadata/Form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public function validateData(array $data)
367367
foreach ($validator->getMessages() as $errorMessages) {
368368
$messages[] = (array)$errorMessages;
369369
}
370-
return array_merge([], ...$messages);
370+
return $messages;
371371
}
372372
return true;
373373
}

app/code/Magento/Customer/Model/Metadata/Form/Postcode.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Customer\Api\Data\AttributeMetadataInterface;
99
use Magento\Customer\Model\Metadata\ElementFactory;
1010
use Magento\Directory\Helper\Data as DirectoryHelper;
11+
use Magento\Framework\Api\ArrayObjectSearch;
1112
use Magento\Framework\Locale\ResolverInterface;
1213
use Psr\Log\LoggerInterface as PsrLogger;
1314
use Magento\Framework\Stdlib\DateTime\TimezoneInterface as MagentoTimezone;
@@ -22,6 +23,11 @@ class Postcode extends AbstractData
2223
*/
2324
protected $directoryHelper;
2425

26+
/**
27+
* @var \Magento\Framework\Stdlib\StringUtils
28+
*/
29+
protected $_string;
30+
2531
/**
2632
* @param MagentoTimezone $localeDate
2733
* @param PsrLogger $logger
@@ -31,6 +37,7 @@ class Postcode extends AbstractData
3137
* @param string $entityTypeCode
3238
* @param bool $isAjax
3339
* @param DirectoryHelper $directoryHelper
40+
* @param \Magento\Framework\Stdlib\StringUtils $stringHelper
3441
*/
3542
public function __construct(
3643
MagentoTimezone $localeDate,
@@ -40,9 +47,11 @@ public function __construct(
4047
$value,
4148
$entityTypeCode,
4249
$isAjax,
43-
DirectoryHelper $directoryHelper
50+
DirectoryHelper $directoryHelper,
51+
\Magento\Framework\Stdlib\StringUtils $stringHelper
4452
) {
4553
$this->directoryHelper = $directoryHelper;
54+
$this->_string = $stringHelper;
4655
parent::__construct(
4756
$localeDate,
4857
$logger,
@@ -75,6 +84,14 @@ public function validateValue($value)
7584
if (empty($value) && $value !== '0') {
7685
$errors[] = __('"%1" is a required value.', $label);
7786
}
87+
88+
$errors = $this->validateLength($value, $attribute, $errors);
89+
90+
$result = $this->_validateInputRule($value);
91+
if ($result !== true) {
92+
$errors = array_merge($errors, $result);
93+
}
94+
7895
if (count($errors) == 0) {
7996
return true;
8097
}
@@ -112,4 +129,42 @@ public function outputValue($format = ElementFactory::OUTPUT_FORMAT_TEXT)
112129
{
113130
return $this->_applyOutputFilter($this->_value);
114131
}
132+
133+
/**
134+
* Length validation
135+
*
136+
* @param mixed $value
137+
* @param AttributeMetadataInterface $attribute
138+
* @param array $errors
139+
* @return array
140+
*/
141+
private function validateLength($value, AttributeMetadataInterface $attribute, array $errors): array
142+
{
143+
// validate length
144+
$label = __($attribute->getStoreLabel());
145+
146+
$length = $value ? $this->_string->strlen(trim($value)) : 0;
147+
148+
$validateRules = $attribute->getValidationRules();
149+
150+
if (!empty(ArrayObjectSearch::getArrayElementByName($validateRules, 'input_validation'))) {
151+
$minTextLength = ArrayObjectSearch::getArrayElementByName(
152+
$validateRules,
153+
'min_text_length'
154+
);
155+
if ($minTextLength !== null && $length < $minTextLength) {
156+
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
157+
}
158+
159+
$maxTextLength = ArrayObjectSearch::getArrayElementByName(
160+
$validateRules,
161+
'max_text_length'
162+
);
163+
if ($maxTextLength !== null && $length > $maxTextLength) {
164+
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
165+
}
166+
}
167+
168+
return $errors;
169+
}
115170
}

0 commit comments

Comments
 (0)