88use Magento \Customer \Api \Data \AttributeMetadataInterface ;
99use Magento \Customer \Model \Metadata \ElementFactory ;
1010use Magento \Directory \Helper \Data as DirectoryHelper ;
11+ use Magento \Framework \Api \ArrayObjectSearch ;
1112use Magento \Framework \Locale \ResolverInterface ;
1213use Psr \Log \LoggerInterface as PsrLogger ;
1314use 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