-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[EAV] Improving the EAV attribute code validation, by not allowing to use n… #20526
Merged
magento-engcom-team
merged 13 commits into
magento:2.3-develop
from
eduard13:2.3-develop-eav-attribute-code-validation
Mar 30, 2019
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6f83604
Improving the eav attribute code validation, by not allowing to use n…
eduard13 adb1a8f
Declaring strict typing
eduard13 3c8acbb
Using an alias for attribute code validator
eduard13 82f5511
Adding returning strict typing
eduard13 4488371
Allowing usage of the capital letters in attribute code
eduard13 e028d6f
Improving the validation rules on product attribute save
eduard13 8129886
Fixing the Regex expr
eduard13 079bb15
Using the same regex rule as it is in attribute code validator
eduard13 5666a85
Merge branch '2.3-develop' into 2.3-develop-eav-attribute-code-valida…
eduard13 50b78da
Refactoring the validator usage. Updating the Unit/Integration tests.…
eduard13 6878668
Merge branch '2.3-develop' into 2.3-develop-eav-attribute-code-valida…
p-bystritsky 40683de
magento/magento2#20526: Absent ObjectManager fix.
p-bystritsky 392cd1b
magento/magento2#20526: Static test fix.
p-bystritsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Eav\Model\Validator\Attribute; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please cover the validator class with a unit test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class was covered by Unit Test, and also all the related tests (unit, integration) were updated. |
||
|
||
use Magento\Eav\Model\Entity\Attribute; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
/** | ||
* Class Code | ||
* | ||
* Validation EAV attribute code | ||
*/ | ||
class Code extends \Magento\Framework\Validator\AbstractValidator | ||
{ | ||
/** | ||
* Validates the correctness of the attribute code | ||
* | ||
* @param $attributeCode | ||
* @return bool | ||
* @throws LocalizedException | ||
*/ | ||
public function isValid($attributeCode) | ||
{ | ||
/** | ||
* Check attribute_code for allowed characters | ||
*/ | ||
if (trim($attributeCode) | ||
&& !preg_match('/^[a-z][a-z0-9_]*$/', trim($attributeCode)) | ||
) { | ||
throw new LocalizedException( | ||
__( | ||
'Attribute code "%1" is invalid. Please use only letters (a-z), ' . | ||
'numbers (0-9) or underscore(_) in this field, first character should be a letter.', | ||
$attributeCode | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Check attribute_code for allowed length | ||
*/ | ||
$minLength = Attribute::ATTRIBUTE_CODE_MIN_LENGTH; | ||
$maxLength = Attribute::ATTRIBUTE_CODE_MAX_LENGTH; | ||
$isAllowedLength = \Zend_Validate::is( | ||
trim($attributeCode), | ||
'StringLength', | ||
['min' => $minLength, 'max' => $maxLength] | ||
); | ||
if (!$isAllowedLength) { | ||
throw new LocalizedException(__( | ||
'An attribute code must not be less than %1 and more than %2 characters.', | ||
$minLength, | ||
$maxLength | ||
)); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good idea to use strict types within a scope of new implementations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, the strict typing was declared.
Also do you think it's a good idea to rename this variable name into a less meaningful name (in order to fix the failing pipeline)?
magento2/app/code/Magento/Eav/Model/Entity/Attribute.php
Line 134 in adb1a8f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have implemented the naming in the same manner as other validators. So, it's ok.
You could use an alias in the
use
section for this class. Something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rogyar the alias was added.