Skip to content

Commit d2c352d

Browse files
committed
Module catalog, Attribute Repository code validation regex
Regex modified to accept the A-Z range and check for Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH instead of the old hardcoded value (30)
1 parent 735579d commit d2c352d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Repository.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Catalog\Model\Product\Attribute;
88

99
use Magento\Eav\Api\Data\AttributeInterface;
10+
use Magento\Eav\Model\Entity\Attribute;
1011
use Magento\Framework\Exception\InputException;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213

@@ -218,8 +219,8 @@ public function getCustomAttributesMetadata($dataObjectClassName = null)
218219
*/
219220
protected function generateCode($label)
220221
{
221-
$code = substr(preg_replace('/[^a-z_0-9]/', '_', $this->filterManager->translitUrl($label)), 0, 30);
222-
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']);
222+
$code = substr(preg_replace('/[^a-zA-Z_0-9]/', '_', $this->filterManager->translitUrl($label)), 0, Attribute::ATTRIBUTE_CODE_MAX_LENGTH);
223+
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-zA-Z][a-zA-Z_0-9]{0,29}[a-zA-Z0-9]$/']);
223224
if (!$validatorAttrCode->isValid($code)) {
224225
$code = 'attr_' . ($code ?: substr(md5(time()), 0, 8));
225226
}
@@ -235,7 +236,7 @@ protected function generateCode($label)
235236
*/
236237
protected function validateCode($code)
237238
{
238-
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,30}$/']);
239+
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-zA-Z][a-zA-Z_0-9]{0,' . Attribute::ATTRIBUTE_CODE_MAX_LENGTH . '}$/']);
239240
if (!$validatorAttrCode->isValid($code)) {
240241
throw InputException::invalidFieldValue('attribute_code', $code);
241242
}

0 commit comments

Comments
 (0)