diff --git a/app/code/Magento/Customer/Helper/Address.php b/app/code/Magento/Customer/Helper/Address.php index e5a20d8f51761..cde9fc903e4d2 100644 --- a/app/code/Magento/Customer/Helper/Address.php +++ b/app/code/Magento/Customer/Helper/Address.php @@ -71,7 +71,11 @@ class Address extends \Magento\Framework\App\Helper\AbstractHelper /** @var \Magento\Store\Model\StoreManagerInterface */ protected $_storeManager; - /** @var CustomerMetadataInterface */ + /** + * @var CustomerMetadataInterface + * + * @deprecated + */ protected $_customerMetadataService; /** @var AddressMetadataInterface */ @@ -243,19 +247,8 @@ public function getAttributeValidationClass($attributeCode) $attribute = isset($this->_attributes[$attributeCode]) ? $this->_attributes[$attributeCode] : $this->_addressMetadataService->getAttributeMetadata($attributeCode); - $class = $attribute ? $attribute->getFrontendClass() : ''; - if (in_array($attributeCode, ['firstname', 'middlename', 'lastname', 'prefix', 'suffix', 'taxvat'])) { - if ($class && !$attribute->isVisible()) { - // address attribute is not visible thus its validation rules are not applied - $class = ''; - } - /** @var $customerAttribute AttributeMetadataInterface */ - $customerAttribute = $this->_customerMetadataService->getAttributeMetadata($attributeCode); - $class .= $customerAttribute && - $customerAttribute->isVisible() ? $customerAttribute->getFrontendClass() : ''; - $class = implode(' ', array_unique(array_filter(explode(' ', $class)))); - } + $class = $attribute ? $attribute->getFrontendClass() : ''; } catch (NoSuchEntityException $e) { // the attribute does not exist so just return an empty string } diff --git a/app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php b/app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php index c5fd88021ef7d..4a28ed7462f21 100644 --- a/app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php +++ b/app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php @@ -147,39 +147,33 @@ public function testGetConfigCanShowConfig() $this->assertTrue($this->helper->canShowConfig('key2')); } - /** - * @param $attrCode - * @param $attrClass - * @param $customAttrClass - * @param $result - * @dataProvider getAttributeValidationClassDataProvider - */ - public function testGetAttributeValidationClass($attrCode, $attrClass, $customAttrClass, $result) + public function testGetAttributeValidationClass() { - $attributeMock = $this->getMockBuilder('Magento\Customer\Api\Data\AttributeMetadataInterface')->getMock(); - $attributeMock->expects($this->any())->method('getFrontendClass')->will($this->returnValue($attrClass)); + $attributeCode = 'attr_code'; + $attributeClass = 'Attribute_Class'; - $customAttrMock = $this->getMockBuilder('Magento\Customer\Api\Data\AttributeMetadataInterface')->getMock(); - $customAttrMock->expects($this->any())->method('isVisible')->will($this->returnValue(true)); - $customAttrMock->expects($this->any())->method('getFrontendClass')->will($this->returnValue($customAttrClass)); - - $this->customerMetadataService->expects($this->any()) - ->method('getAttributeMetadata') - ->will($this->returnValue($customAttrMock)); + $attributeMock = $this->getMockBuilder('Magento\Customer\Api\Data\AttributeMetadataInterface') + ->getMockForAbstractClass(); + $attributeMock->expects($this->once()) + ->method('getFrontendClass') + ->willReturn($attributeClass); $this->addressMetadataService->expects($this->any()) ->method('getAttributeMetadata') - ->will($this->returnValue($attributeMock)); + ->willReturn($attributeMock); - $this->assertEquals($result, $this->helper->getAttributeValidationClass($attrCode)); + $this->assertEquals($attributeClass, $this->helper->getAttributeValidationClass($attributeCode)); } - public function getAttributeValidationClassDataProvider() + public function testGetAttributeValidationClassWithNoAttribute() { - return [ - ['attr_code', 'Attribute_Class', '', 'Attribute_Class'], - ['firstname', 'Attribute_Class', 'Attribute2_Class', 'Attribute2_Class'], - ]; + $attrCode = 'attr_code'; + + $this->addressMetadataService->expects($this->any()) + ->method('getAttributeMetadata') + ->willReturn(null); + + $this->assertEquals('', $this->helper->getAttributeValidationClass($attrCode)); } /** diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php b/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php index 98d6086190047..be07557f59fc0 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php @@ -155,11 +155,13 @@ public function getClass() if ($inputRuleClass) { $out[] = $inputRuleClass; } - if (!empty($out)) { - $out = implode(' ', $out); - } else { - $out = ''; + + $textLengthValidateClasses = $this->getTextLengthValidateClasses(); + if (!empty($textLengthValidateClasses)) { + $out[] = implode(' ', $textLengthValidateClasses); } + + $out = !empty($out) ? implode(' ', $out) : ''; return $out; } @@ -197,6 +199,29 @@ protected function _getInputValidateClass() return $class; } + /** + * Retrieve validation classes by min_text_length and max_text_length rules + * + * @return array + */ + private function getTextLengthValidateClasses() + { + $classes = []; + + $validateRules = $this->getAttribute()->getValidateRules(); + if (!empty($validateRules['min_text_length'])) { + $classes[] = 'minimum-length-' . $validateRules['min_text_length']; + } + if (!empty($validateRules['max_text_length'])) { + $classes[] = 'maximum-length-' . $validateRules['max_text_length']; + } + if (!empty($classes)) { + $classes[] = 'validate-length'; + } + + return $classes; + } + /** * Reireive config field * diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Frontend/DefaultFrontendTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Frontend/DefaultFrontendTest.php new file mode 100644 index 0000000000000..2b4ec6a7686bf --- /dev/null +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Frontend/DefaultFrontendTest.php @@ -0,0 +1,91 @@ +booleanFactory = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory') + ->disableOriginalConstructor() + ->getMock(); + + $this->model = new DefaultFrontend( + $this->booleanFactory + ); + } + + public function testGetClassEmpty() + { + /** @var \PHPUnit_Framework_MockObject_MockObject $attributeMock */ + $attributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute') + ->disableOriginalConstructor() + ->setMethods([ + 'getIsRequired', + 'getFrontendClass', + 'getValidateRules', + ]) + ->getMock(); + $attributeMock->expects($this->once()) + ->method('getIsRequired') + ->willReturn(false); + $attributeMock->expects($this->once()) + ->method('getFrontendClass') + ->willReturn(''); + $attributeMock->expects($this->exactly(2)) + ->method('getValidateRules') + ->willReturn(''); + + $this->model->setAttribute($attributeMock); + $this->assertEmpty($this->model->getClass()); + } + + public function testGetClass() + { + /** @var \PHPUnit_Framework_MockObject_MockObject $attributeMock */ + $attributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute') + ->disableOriginalConstructor() + ->setMethods([ + 'getIsRequired', + 'getFrontendClass', + 'getValidateRules', + ]) + ->getMock(); + $attributeMock->expects($this->once()) + ->method('getIsRequired') + ->willReturn(true); + $attributeMock->expects($this->once()) + ->method('getFrontendClass') + ->willReturn(''); + $attributeMock->expects($this->exactly(2)) + ->method('getValidateRules') + ->willReturn([ + 'input_validation' => 'alphanumeric', + 'min_text_length' => 1, + 'max_text_length' => 2, + ]); + + $this->model->setAttribute($attributeMock); + $result = $this->model->getClass(); + + $this->assertContains('validate-alphanum', $result); + $this->assertContains('minimum-length-1', $result); + $this->assertContains('maximum-length-2', $result); + $this->assertContains('validate-length', $result); + } +} diff --git a/lib/web/mage/validation.js b/lib/web/mage/validation.js index aff3141c2d4d1..edd3b9af42fe1 100644 --- a/lib/web/mage/validation.js +++ b/lib/web/mage/validation.js @@ -1137,18 +1137,22 @@ $.each(elm.className.split(' '), function (index, name) { if (name.match(reMax) && result) { length = name.split('-')[2]; - validator.attrLength = length; result = (v.length <= length); + validator.validateMessage = $.mage.__( + "Please enter less or equal than %1 symbols." + ).replace('%1', length); } - if (name.match(reMin) && result && $.mage.isEmpty(v)) { + if (name.match(reMin) && result && !$.mage.isEmpty(v)) { length = name.split('-')[2]; result = v.length >= length; + validator.validateMessage = $.mage.__( + "Please enter more or equal than %1 symbols." + ).replace('%1', length); } }); return result; }, function () { - return $.mage.__("Maximum length of this field must be equal or less than %1 symbols.") - .replace('%1', this.attrLength); + return this.validateMessage; } ], 'required-entry': [