From 5a9a9e682f38ea2926488f0e79c4744307a690f2 Mon Sep 17 00:00:00 2001 From: rajneesh1dev Date: Thu, 31 Jan 2019 12:31:13 +0530 Subject: [PATCH] added check for attribute code length and error message --- .../Adminhtml/Product/Attribute/Save.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Save.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Save.php index 39ed11b1806cd..c96d1424132c4 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Save.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Save.php @@ -195,9 +195,26 @@ public function execute() ? $model->getAttributeCode() : $this->getRequest()->getParam('attribute_code'); $attributeCode = $attributeCode ?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]); + $maxLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH; + $minLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MIN_LENGTH; + if (strlen($attributeCode) > $maxLength) { + $this->messageManager->addErrorMessage( + __( + 'Attribute code "%1" length should not greater than "%2" characters.' , + $attributeCode, + $maxLength + ) + ); + return $this->returnResult( + 'catalog/*/edit', + ['attribute_id' => $attributeId, '_current' => true], + ['error' => true] + ); + } if (strlen($attributeCode) > 0) { + $pattern = sprintf('/^[a-zA-Z\x{600}-\x{6FF}][a-zA-Z\x{600}-\x{6FF}_0-9]{%s,%s}$/u', $minLength , $maxLength); $validatorAttrCode = new \Zend_Validate_Regex( - ['pattern' => '/^[a-zA-Z\x{600}-\x{6FF}][a-zA-Z\x{600}-\x{6FF}_0-9]{0,30}$/u'] + ['pattern' => $pattern] ); if (!$validatorAttrCode->isValid($attributeCode)) { $this->messageManager->addErrorMessage(