-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGETWO-54780: [GitHub] Validate attribute values #4881
- Loading branch information
Sergey Shvets
committed
Jul 15, 2016
1 parent
4d8ec01
commit 8241423
Showing
11 changed files
with
532 additions
and
12 deletions.
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
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
46 changes: 46 additions & 0 deletions
46
app/code/Magento/Catalog/view/adminhtml/web/catalog/product/attribute/unique-validate.js
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,46 @@ | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
/* global $break $ $$ */ | ||
|
||
define([ | ||
'jquery', | ||
'mage/backend/validation' | ||
], function (jQuery) { | ||
'use strict'; | ||
|
||
return function (config) { | ||
var _config = jQuery.extend({ | ||
element: null, | ||
message: "", | ||
uniqueClass: 'required-unique' | ||
}, config); | ||
|
||
if (typeof _config.element === "string") { | ||
jQuery.validator.addMethod( | ||
_config.element, | ||
|
||
function (value, element) { | ||
var inputs = jQuery(element) | ||
.closest("table") | ||
.find('.' + _config.uniqueClass + ":visible"), | ||
valuesHash = {}, | ||
isValid = true; | ||
|
||
inputs.each(function (el) { | ||
var inputValue = inputs[el].value; | ||
if (typeof valuesHash[inputValue] !== "undefined") { | ||
isValid = false; | ||
} | ||
valuesHash[inputValue] = el; | ||
}); | ||
return isValid; | ||
}, | ||
|
||
_config.message | ||
); | ||
} | ||
}; | ||
}); |
81 changes: 81 additions & 0 deletions
81
app/code/Magento/Swatches/Controller/Adminhtml/Product/Attribute/Plugin/Validate.php
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,81 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Swatches\Controller\Adminhtml\Product\Attribute\Plugin; | ||
|
||
use Magento\Catalog\Controller\Adminhtml\Product\Attribute; | ||
use Magento\Framework\Controller\Result\Json; | ||
use Magento\Framework\DataObject; | ||
|
||
/** | ||
* Class Save | ||
* @package Magento\Swatches\Controller\Adminhtml\Product\Attribute\Plugin | ||
*/ | ||
class Validate | ||
{ | ||
/** | ||
* @param Attribute\Validate $subject | ||
* @param Json $response | ||
* @return array | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterExecute(Attribute\Validate $subject, Json $response) | ||
{ | ||
$data = $subject->getRequest()->getPostValue(); | ||
if (isset($data['frontend_input'])) { | ||
$dataIndex = false; | ||
switch ($data['frontend_input']) { | ||
case "select": | ||
$dataIndex = "option"; | ||
break; | ||
case "swatch_text": | ||
$dataIndex = "optiontext"; | ||
break; | ||
case "swatch_visual": | ||
$dataIndex = "optionvisual"; | ||
break; | ||
} | ||
|
||
if ($dataIndex !== false) { | ||
if (!$this->isUniqueAdminValues($data[$dataIndex]['value'], $data[$dataIndex]['delete'])) { | ||
$response->setJsonData($this->createErrorResponse("The value of Admin must be unique.")->toJson()); | ||
}; | ||
} | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Throws Exception if not unique values into options | ||
* @param array $optionsValues | ||
* @param array $deletedOptions | ||
* @return bool | ||
*/ | ||
private function isUniqueAdminValues(array $optionsValues, array $deletedOptions) | ||
{ | ||
$adminValues = array(); | ||
foreach ($optionsValues as $optionKey => $values) { | ||
if (!(isset($deletedOptions[$optionKey]) and $deletedOptions[$optionKey] === '1')) { | ||
$adminValues[] = reset($values); | ||
} | ||
} | ||
$uniqueValues = array_unique($adminValues); | ||
return ($uniqueValues === $adminValues); | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @return DataObject | ||
*/ | ||
private function createErrorResponse($message) | ||
{ | ||
$error = new DataObject(); | ||
$error->setError(true); | ||
$error->setData(Attribute\Validate ::DEFAULT_MESSAGE_KEY, __($message)); | ||
return $error; | ||
} | ||
} |
Oops, something went wrong.