8
8
use Magento \Customer \Api \Data \AttributeMetadataInterface ;
9
9
use Magento \Customer \Model \Metadata \ElementFactory ;
10
10
use Magento \Directory \Helper \Data as DirectoryHelper ;
11
+ use Magento \Framework \Api \ArrayObjectSearch ;
11
12
use Magento \Framework \Locale \ResolverInterface ;
12
13
use Psr \Log \LoggerInterface as PsrLogger ;
13
14
use Magento \Framework \Stdlib \DateTime \TimezoneInterface as MagentoTimezone ;
@@ -22,6 +23,11 @@ class Postcode extends AbstractData
22
23
*/
23
24
protected $ directoryHelper ;
24
25
26
+ /**
27
+ * @var \Magento\Framework\Stdlib\StringUtils
28
+ */
29
+ protected $ _string ;
30
+
25
31
/**
26
32
* @param MagentoTimezone $localeDate
27
33
* @param PsrLogger $logger
@@ -31,6 +37,7 @@ class Postcode extends AbstractData
31
37
* @param string $entityTypeCode
32
38
* @param bool $isAjax
33
39
* @param DirectoryHelper $directoryHelper
40
+ * @param \Magento\Framework\Stdlib\StringUtils $stringHelper
34
41
*/
35
42
public function __construct (
36
43
MagentoTimezone $ localeDate ,
@@ -40,9 +47,11 @@ public function __construct(
40
47
$ value ,
41
48
$ entityTypeCode ,
42
49
$ isAjax ,
43
- DirectoryHelper $ directoryHelper
50
+ DirectoryHelper $ directoryHelper ,
51
+ \Magento \Framework \Stdlib \StringUtils $ stringHelper
44
52
) {
45
53
$ this ->directoryHelper = $ directoryHelper ;
54
+ $ this ->_string = $ stringHelper ;
46
55
parent ::__construct (
47
56
$ localeDate ,
48
57
$ logger ,
@@ -75,6 +84,14 @@ public function validateValue($value)
75
84
if (empty ($ value ) && $ value !== '0 ' ) {
76
85
$ errors [] = __ ('"%1" is a required value. ' , $ label );
77
86
}
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
+
78
95
if (count ($ errors ) == 0 ) {
79
96
return true ;
80
97
}
@@ -112,4 +129,42 @@ public function outputValue($format = ElementFactory::OUTPUT_FORMAT_TEXT)
112
129
{
113
130
return $ this ->_applyOutputFilter ($ this ->_value );
114
131
}
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
+ }
115
170
}
0 commit comments