Skip to content

Commit faa6814

Browse files
author
Stanislav Idolov
authored
ENGCOM-2998: Adding trimming sku value function to sku backend model. #18019
2 parents 015b131 + 9d824af commit faa6814

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Backend/Sku.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
/**
8-
* Catalog product SKU backend attribute model
9-
*
10-
* @author Magento Core Team <[email protected]>
11-
*/
127
namespace Magento\Catalog\Model\Product\Attribute\Backend;
138

149
use Magento\Catalog\Model\Product;
1510

11+
/**
12+
* Catalog product SKU backend attribute model.
13+
*/
1614
class Sku extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
1715
{
1816
/**
@@ -97,6 +95,7 @@ protected function _generateUniqueSku($object)
9795
public function beforeSave($object)
9896
{
9997
$this->_generateUniqueSku($object);
98+
$this->trimValue($object);
10099
return parent::beforeSave($object);
101100
}
102101

@@ -127,4 +126,19 @@ protected function _getLastSimilarAttributeValueIncrement($attribute, $object)
127126
$data = $connection->fetchOne($select, $bind);
128127
return abs((int)str_replace($value, '', $data));
129128
}
129+
130+
/**
131+
* Remove extra spaces from attribute value before save.
132+
*
133+
* @param Product $object
134+
* @return void
135+
*/
136+
private function trimValue($object)
137+
{
138+
$attrCode = $this->getAttribute()->getAttributeCode();
139+
$value = $object->getData($attrCode);
140+
if ($value) {
141+
$object->setData($attrCode, trim($value));
142+
}
143+
}
130144
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/General.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ public function __construct(
5858
}
5959

6060
/**
61-
* {@inheritdoc}
61+
* Customize number fields for advanced price and weight fields.
62+
*
6263
* @since 101.0.0
64+
* @param array $data
65+
* @return array
6366
* @throws \Magento\Framework\Exception\NoSuchEntityException
6467
*/
6568
public function modifyData(array $data)
@@ -130,8 +133,11 @@ protected function customizeAdvancedPriceFormat(array $data)
130133
}
131134

132135
/**
133-
* {@inheritdoc}
136+
* Customize product form fields.
137+
*
134138
* @since 101.0.0
139+
* @param array $meta
140+
* @return array
135141
*/
136142
public function modifyMeta(array $meta)
137143
{
@@ -361,7 +367,8 @@ protected function customizeNameListeners(array $meta)
361367
$skuPath . static::META_CONFIG_PATH,
362368
$meta,
363369
[
364-
'autoImportIfEmpty' => true
370+
'autoImportIfEmpty' => true,
371+
'validation' => ['no-marginal-whitespace' => true]
365372
]
366373
);
367374

app/code/Magento/Ui/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Keyword,Keyword
5858
"Letters, numbers, spaces or underscores only please","Letters, numbers, spaces or underscores only please"
5959
"Letters only please","Letters only please"
6060
"No white space please","No white space please"
61+
"No marginal white space please","No marginal white space please"
6162
"Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx","Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx"
6263
"A positive or negative non-decimal number please","A positive or negative non-decimal number please"
6364
"The specified vehicle identification number (VIN) is invalid.","The specified vehicle identification number (VIN) is invalid."

app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ define([
116116
},
117117
$.mage.__('No white space please')
118118
],
119+
'no-marginal-whitespace': [
120+
function (value) {
121+
return !/^\s+|\s+$/i.test(value);
122+
},
123+
$.mage.__('No marginal white space please')
124+
],
119125
'zip-range': [
120126
function (value) {
121127
return utils.isEmpty(value) || /^90[2-5]-\d{2}-\d{4}$/.test(value);

lib/web/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Submit,Submit
2727
"Letters, numbers, spaces or underscores only please","Letters, numbers, spaces or underscores only please"
2828
"Letters only please","Letters only please"
2929
"No white space please","No white space please"
30+
"No marginal white space please","No marginal white space please"
3031
"Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx","Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx"
3132
"A positive or negative non-decimal number please","A positive or negative non-decimal number please"
3233
"The specified vehicle identification number (VIN) is invalid.","The specified vehicle identification number (VIN) is invalid."

lib/web/mage/validation.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@
253253
},
254254
$.mage.__('No white space please')
255255
],
256+
'no-marginal-whitespace': [
257+
function (value, element) {
258+
return this.optional(element) || !/^\s+|\s+$/i.test(value);
259+
},
260+
$.mage.__('No marginal white space please')
261+
],
256262
'zip-range': [
257263
function (value, element) {
258264
return this.optional(element) || /^90[2-5]-\d{2}-\d{4}$/.test(value);

0 commit comments

Comments
 (0)