Skip to content

Commit 5ffe84d

Browse files
author
Sergey Shvets
committed
MAGETWO-86239: Customer Address attribute value length is still validated when min/max length fields are not displayed at the backend
1 parent 209c713 commit 5ffe84d

File tree

1 file changed

+43
-22
lines changed
  • app/code/Magento/Customer/Model/Metadata/Form

1 file changed

+43
-22
lines changed

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

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
89
namespace Magento\Customer\Model\Metadata\Form;
910

11+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1012
use Magento\Framework\Api\ArrayObjectSearch;
1113

1214
class Text extends AbstractData
@@ -19,7 +21,7 @@ class Text extends AbstractData
1921
/**
2022
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
2123
* @param \Psr\Log\LoggerInterface $logger
22-
* @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
24+
* @param AttributeMetadataInterface $attribute
2325
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
2426
* @param string $value
2527
* @param string $entityTypeCode
@@ -29,7 +31,7 @@ class Text extends AbstractData
2931
public function __construct(
3032
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
3133
\Psr\Log\LoggerInterface $logger,
32-
\Magento\Customer\Api\Data\AttributeMetadataInterface $attribute,
34+
AttributeMetadataInterface $attribute,
3335
\Magento\Framework\Locale\ResolverInterface $localeResolver,
3436
$value,
3537
$entityTypeCode,
@@ -72,26 +74,7 @@ public function validateValue($value)
7274
return true;
7375
}
7476

75-
// validate length
76-
$length = $this->_string->strlen(trim($value));
77-
78-
$validateRules = $attribute->getValidationRules();
79-
80-
$minTextLength = ArrayObjectSearch::getArrayElementByName(
81-
$validateRules,
82-
'min_text_length'
83-
);
84-
if ($minTextLength !== null && $length < $minTextLength) {
85-
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
86-
}
87-
88-
$maxTextLength = ArrayObjectSearch::getArrayElementByName(
89-
$validateRules,
90-
'max_text_length'
91-
);
92-
if ($maxTextLength !== null && $length > $maxTextLength) {
93-
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
94-
}
77+
$errors = $this->validateLength($value, $attribute, $errors);
9578

9679
$result = $this->_validateInputRule($value);
9780
if ($result !== true) {
@@ -127,4 +110,42 @@ public function outputValue($format = \Magento\Customer\Model\Metadata\ElementFa
127110
{
128111
return $this->_applyOutputFilter($this->_value);
129112
}
113+
114+
/**
115+
* Length validation
116+
*
117+
* @param mixed $value
118+
* @param AttributeMetadataInterface $attribute
119+
* @param array $errors
120+
* @return array
121+
*/
122+
protected function validateLength($value, AttributeMetadataInterface $attribute, array $errors): array
123+
{
124+
// validate length
125+
$label = __($attribute->getStoreLabel());
126+
127+
$length = $this->_string->strlen(trim($value));
128+
129+
$validateRules = $attribute->getValidationRules();
130+
131+
if (!empty(ArrayObjectSearch::getArrayElementByName($validateRules, 'input_validation'))) {
132+
$minTextLength = ArrayObjectSearch::getArrayElementByName(
133+
$validateRules,
134+
'min_text_length'
135+
);
136+
if ($minTextLength !== null && $length < $minTextLength) {
137+
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
138+
}
139+
140+
$maxTextLength = ArrayObjectSearch::getArrayElementByName(
141+
$validateRules,
142+
'max_text_length'
143+
);
144+
if ($maxTextLength !== null && $length > $maxTextLength) {
145+
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
146+
}
147+
}
148+
149+
return $errors;
150+
}
130151
}

0 commit comments

Comments
 (0)