5
5
* Copyright © Magento, Inc. All rights reserved.
6
6
* See COPYING.txt for license details.
7
7
*/
8
+
8
9
namespace Magento \Customer \Model \Metadata \Form ;
9
10
11
+ use Magento \Customer \Api \Data \AttributeMetadataInterface ;
10
12
use Magento \Framework \Api \ArrayObjectSearch ;
11
13
12
14
class Text extends AbstractData
@@ -19,7 +21,7 @@ class Text extends AbstractData
19
21
/**
20
22
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
21
23
* @param \Psr\Log\LoggerInterface $logger
22
- * @param \Magento\Customer\Api\Data\ AttributeMetadataInterface $attribute
24
+ * @param AttributeMetadataInterface $attribute
23
25
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
24
26
* @param string $value
25
27
* @param string $entityTypeCode
@@ -29,7 +31,7 @@ class Text extends AbstractData
29
31
public function __construct (
30
32
\Magento \Framework \Stdlib \DateTime \TimezoneInterface $ localeDate ,
31
33
\Psr \Log \LoggerInterface $ logger ,
32
- \ Magento \ Customer \ Api \ Data \ AttributeMetadataInterface $ attribute ,
34
+ AttributeMetadataInterface $ attribute ,
33
35
\Magento \Framework \Locale \ResolverInterface $ localeResolver ,
34
36
$ value ,
35
37
$ entityTypeCode ,
@@ -72,26 +74,7 @@ public function validateValue($value)
72
74
return true ;
73
75
}
74
76
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 );
95
78
96
79
$ result = $ this ->_validateInputRule ($ value );
97
80
if ($ result !== true ) {
@@ -127,4 +110,42 @@ public function outputValue($format = \Magento\Customer\Model\Metadata\ElementFa
127
110
{
128
111
return $ this ->_applyOutputFilter ($ this ->_value );
129
112
}
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
+ }
130
151
}
0 commit comments