Skip to content

Commit 1dcb806

Browse files
author
olysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-86239' into chaika_pr
2 parents 95891c8 + 232b210 commit 1dcb806

File tree

10 files changed

+247
-98
lines changed

10 files changed

+247
-98
lines changed

app/code/Magento/Customer/Controller/Address/FormPost.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,17 @@ public function execute()
197197
try {
198198
$address = $this->_extractAddress();
199199
$this->_addressRepository->save($address);
200-
$this->messageManager->addSuccess(__('You saved the address.'));
200+
$this->messageManager->addSuccessMessage(__('You saved the address.'));
201201
$url = $this->_buildUrl('*/*/index', ['_secure' => true]);
202202
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->success($url));
203203
} catch (InputException $e) {
204-
$this->messageManager->addError($e->getMessage());
204+
$this->messageManager->addErrorMessage($e->getMessage());
205205
foreach ($e->getErrors() as $error) {
206-
$this->messageManager->addError($error->getMessage());
206+
$this->messageManager->addErrorMessage($error->getMessage());
207207
}
208208
} catch (\Exception $e) {
209209
$redirectUrl = $this->_buildUrl('*/*/index');
210-
$this->messageManager->addException($e, __('We can\'t save the address.'));
210+
$this->messageManager->addExceptionMessage($e, __('We can\'t save the address.'));
211211
}
212212

213213
$url = $redirectUrl;

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
}

app/code/Magento/Customer/Test/Unit/Controller/Address/FormPostTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ public function testExecute(
549549
->willReturnSelf();
550550

551551
$this->messageManager->expects($this->once())
552-
->method('addSuccess')
552+
->method('addSuccessMessage')
553553
->with(__('You saved the address.'))
554554
->willReturnSelf();
555555

@@ -640,7 +640,7 @@ public function testExecuteInputException()
640640
->willThrowException(new InputException(__('InputException')));
641641

642642
$this->messageManager->expects($this->once())
643-
->method('addError')
643+
->method('addErrorMessage')
644644
->with('InputException')
645645
->willReturnSelf();
646646

@@ -703,7 +703,7 @@ public function testExecuteException()
703703
->willThrowException($exception);
704704

705705
$this->messageManager->expects($this->once())
706-
->method('addException')
706+
->method('addExceptionMessage')
707707
->with($exception, __('We can\'t save the address.'))
708708
->willReturnSelf();
709709

app/code/Magento/Customer/Test/Unit/Model/Metadata/Form/TextTest.php

Lines changed: 16 additions & 2 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\Test\Unit\Model\Metadata\Form;
910

11+
use Magento\Customer\Api\Data\ValidationRuleInterface;
1012
use Magento\Customer\Model\Metadata\Form\Text;
1113

1214
class TextTest extends AbstractFormTestCase
@@ -111,7 +113,7 @@ public function validateValueRequiredDataProvider()
111113
*/
112114
public function testValidateValueLength($value, $expected)
113115
{
114-
$minTextLengthRule = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
116+
$minTextLengthRule = $this->getMockBuilder(ValidationRuleInterface::class)
115117
->disableOriginalConstructor()
116118
->setMethods(['getName', 'getValue'])
117119
->getMockForAbstractClass();
@@ -122,7 +124,7 @@ public function testValidateValueLength($value, $expected)
122124
->method('getValue')
123125
->will($this->returnValue(4));
124126

125-
$maxTextLengthRule = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
127+
$maxTextLengthRule = $this->getMockBuilder(ValidationRuleInterface::class)
126128
->disableOriginalConstructor()
127129
->setMethods(['getName', 'getValue'])
128130
->getMockForAbstractClass();
@@ -133,7 +135,19 @@ public function testValidateValueLength($value, $expected)
133135
->method('getValue')
134136
->will($this->returnValue(8));
135137

138+
$inputValidationRule = $this->getMockBuilder(ValidationRuleInterface::class)
139+
->disableOriginalConstructor()
140+
->setMethods(['getName', 'getValue'])
141+
->getMockForAbstractClass();
142+
$inputValidationRule->expects($this->any())
143+
->method('getName')
144+
->will($this->returnValue('input_validation'));
145+
$inputValidationRule->expects($this->any())
146+
->method('getValue')
147+
->will($this->returnValue('other'));
148+
136149
$validationRules = [
150+
'input_validation' => $inputValidationRule,
137151
'min_text_length' => $minTextLengthRule,
138152
'max_text_length' => $maxTextLengthRule,
139153
];

app/code/Magento/Eav/Model/Attribute/Data/Text.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Eav\Model\Attribute\Data;
78

89
use Magento\Framework\App\RequestInterface;
@@ -76,19 +77,9 @@ public function validateValue($value)
7677
return true;
7778
}
7879

79-
// validate length
80-
$length = $this->_string->strlen(trim($value));
81-
82-
$validateRules = $attribute->getValidateRules();
83-
if (!empty($validateRules['min_text_length']) && $length < $validateRules['min_text_length']) {
84-
$label = __($attribute->getStoreLabel());
85-
$v = $validateRules['min_text_length'];
86-
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $v);
87-
}
88-
if (!empty($validateRules['max_text_length']) && $length > $validateRules['max_text_length']) {
89-
$label = __($attribute->getStoreLabel());
90-
$v = $validateRules['max_text_length'];
91-
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $v);
80+
$result = $this->validateLength($attribute, $value);
81+
if (count($result) !== 0) {
82+
$errors = array_merge($errors, $result);
9283
}
9384

9485
$result = $this->_validateInputRule($value);
@@ -142,4 +133,33 @@ public function outputValue($format = \Magento\Eav\Model\AttributeDataFactory::O
142133

143134
return $value;
144135
}
136+
137+
/**
138+
* Validates value length by attribute rules
139+
*
140+
* @param \Magento\Eav\Model\Attribute $attribute
141+
* @param string $value
142+
* @return array errors
143+
*/
144+
private function validateLength(\Magento\Eav\Model\Attribute $attribute, $value): array
145+
{
146+
$errors = [];
147+
$length = $this->_string->strlen(trim($value));
148+
$validateRules = $attribute->getValidateRules();
149+
150+
if (!empty($validateRules['input_validation'])) {
151+
if (!empty($validateRules['min_text_length']) && $length < $validateRules['min_text_length']) {
152+
$label = __($attribute->getStoreLabel());
153+
$v = $validateRules['min_text_length'];
154+
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $v);
155+
}
156+
if (!empty($validateRules['max_text_length']) && $length > $validateRules['max_text_length']) {
157+
$label = __($attribute->getStoreLabel());
158+
$v = $validateRules['max_text_length'];
159+
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $v);
160+
}
161+
}
162+
163+
return $errors;
164+
}
145165
}

0 commit comments

Comments
 (0)