Skip to content

Commit e028d6f

Browse files
committed
Improving the validation rules on product attribute save
1 parent 4488371 commit e028d6f

File tree

1 file changed

+20
-1
lines changed
  • app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute

1 file changed

+20
-1
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Save.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,23 @@ public function execute()
196196
: $this->getRequest()->getParam('attribute_code');
197197
$attributeCode = $attributeCode ?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]);
198198
if (strlen($attributeCode) > 0) {
199+
$attributeCodeIsValid = true;
200+
$minLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MIN_LENGTH;
201+
$maxLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH;
202+
203+
if (strlen($attributeCode) < $minLength || strlen($attributeCode) > $maxLength) {
204+
$this->messageManager->addErrorMessage(
205+
__(
206+
'An attribute code must not be less than %1 and more than %2 characters.',
207+
$minLength,
208+
$maxLength
209+
)
210+
);
211+
$attributeCodeIsValid = false;
212+
}
213+
199214
$validatorAttrCode = new \Zend_Validate_Regex(
200-
['pattern' => '/^[a-zA-Z\x{600}-\x{6FF}][a-zA-Z\x{600}-\x{6FF}_0-9]{0,30}$/u']
215+
['pattern' => '/^[a-zA-Z]+[a-zA-Z0-9_]$/u']
201216
);
202217
if (!$validatorAttrCode->isValid($attributeCode)) {
203218
$this->messageManager->addErrorMessage(
@@ -207,6 +222,10 @@ public function execute()
207222
$attributeCode
208223
)
209224
);
225+
$attributeCodeIsValid = false;
226+
}
227+
228+
if (!$attributeCodeIsValid) {
210229
return $this->returnResult(
211230
'catalog/*/edit',
212231
['attribute_id' => $attributeId, '_current' => true],

0 commit comments

Comments
 (0)