Skip to content

Commit

Permalink
Improving the validation rules on product attribute save
Browse files Browse the repository at this point in the history
  • Loading branch information
eduard13 committed Feb 6, 2019
1 parent 4488371 commit e028d6f
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,23 @@ public function execute()
: $this->getRequest()->getParam('attribute_code');
$attributeCode = $attributeCode ?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]);
if (strlen($attributeCode) > 0) {
$attributeCodeIsValid = true;
$minLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MIN_LENGTH;
$maxLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH;

if (strlen($attributeCode) < $minLength || strlen($attributeCode) > $maxLength) {
$this->messageManager->addErrorMessage(
__(
'An attribute code must not be less than %1 and more than %2 characters.',
$minLength,
$maxLength
)
);
$attributeCodeIsValid = false;
}

$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' => '/^[a-zA-Z]+[a-zA-Z0-9_]$/u']
);
if (!$validatorAttrCode->isValid($attributeCode)) {
$this->messageManager->addErrorMessage(
Expand All @@ -207,6 +222,10 @@ public function execute()
$attributeCode
)
);
$attributeCodeIsValid = false;
}

if (!$attributeCodeIsValid) {
return $this->returnResult(
'catalog/*/edit',
['attribute_id' => $attributeId, '_current' => true],
Expand Down

0 comments on commit e028d6f

Please sign in to comment.