Skip to content

Commit b4c8b62

Browse files
ENGCOM-4246: [EAV] Improving the EAV attribute code validation, by not allowing to use n… #20526
- Merge Pull Request #20526 from eduard13/magento2:2.3-develop-eav-attribute-code-validation - Merged commits: 1. 6f83604 2. adb1a8f 3. 3c8acbb 4. 82f5511 5. 4488371 6. e028d6f 7. 8129886 8. 079bb15 9. 5666a85 10. 50b78da 11. 6878668 12. 40683de 13. 392cd1b
2 parents 2ef736f + 392cd1b commit b4c8b62

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
namespace Magento\Catalog\Controller\Adminhtml\Product\Attribute;
99

10+
use Magento\Catalog\Controller\Adminhtml\Product\Attribute as AttributeAction;
1011
use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
11-
use Magento\Framework\Serialize\Serializer\FormData;
1212
use Magento\Framework\App\Action\HttpGetActionInterface;
1313
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1414
use Magento\Framework\App\ObjectManager;
1515
use Magento\Framework\DataObject;
16-
use Magento\Catalog\Controller\Adminhtml\Product\Attribute as AttributeAction;
16+
use Magento\Framework\Serialize\Serializer\FormData;
1717

1818
/**
1919
* Product attribute validate controller.
@@ -80,7 +80,8 @@ public function __construct(
8080
$this->formDataSerializer = $formDataSerializer ?: ObjectManager::getInstance()
8181
->get(FormData::class);
8282
$this->attributeCodeValidator = $attributeCodeValidator ?: ObjectManager::getInstance()->get(
83-
AttributeCodeValidator::class);
83+
AttributeCodeValidator::class
84+
);
8485
}
8586

8687
/**

app/code/Magento/Eav/Model/Entity/Attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
106106
* @param DateTimeFormatterInterface $dateTimeFormatter
107107
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
108108
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
109-
* @param AttributeCodeValidator|null $attributeCodeValidator
110109
* @param array $data
110+
* @param AttributeCodeValidator|null $attributeCodeValidator
111111
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
112112
* @codeCoverageIgnore
113113
*/

app/code/Magento/Eav/Setup/EavSetup.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ private function _getValue($array, $key, $default = null)
799799
* @param array $attr
800800
* @return $this
801801
* @throws LocalizedException
802+
* @throws \Zend_Validate_Exception
802803
*/
803804
public function addAttribute($entityTypeId, $code, array $attr)
804805
{
@@ -809,12 +810,7 @@ public function addAttribute($entityTypeId, $code, array $attr)
809810
$this->attributeMapper->map($attr, $entityTypeId)
810811
);
811812

812-
$attributeCode = isset($data['attribute_code']) ? $data['attribute_code'] : '';
813-
if (!$this->attributeCodeValidator->isValid($attributeCode)) {
814-
$errorMessage = implode("\n", $this->attributeCodeValidator->getMessages());
815-
816-
throw new LocalizedException(__($errorMessage));
817-
}
813+
$this->validateAttributeCode($data);
818814

819815
$sortOrder = isset($attr['sort_order']) ? $attr['sort_order'] : null;
820816
$attributeId = $this->getAttribute($entityTypeId, $code, 'attribute_id');
@@ -1535,4 +1531,21 @@ private function _insertAttributeAdditionalData($entityTypeId, array $data)
15351531

15361532
return $this;
15371533
}
1534+
1535+
/**
1536+
* Validate attribute code.
1537+
*
1538+
* @param array $data
1539+
* @throws LocalizedException
1540+
* @throws \Zend_Validate_Exception
1541+
*/
1542+
private function validateAttributeCode(array $data): void
1543+
{
1544+
$attributeCode = $data['attribute_code'] ?? '';
1545+
if (!$this->attributeCodeValidator->isValid($attributeCode)) {
1546+
$errorMessage = implode('\n', $this->attributeCodeValidator->getMessages());
1547+
1548+
throw new LocalizedException(__($errorMessage));
1549+
}
1550+
}
15381551
}

0 commit comments

Comments
 (0)