Skip to content

Commit

Permalink
MAGETWO-54733: Unable to save product with all unchecked values for m…
Browse files Browse the repository at this point in the history
…ultiple select attribute #7687
  • Loading branch information
VladimirZaets committed Jan 16, 2017
1 parent 1856c28 commit 85db4c8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function validate($object)
$data = $object->getData($attributeCode);
if (is_array($data)) {
$object->setData($attributeCode, implode(',', array_filter($data)));
} elseif (empty($data)) {
$object->setData($attributeCode, null);
}
return parent::validate($object);
}
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Ui/view/base/web/js/form/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ define([
function beforeSave(data, url, selectorPrefix, messagesClass) {
var save = $.Deferred();

data = utils.serialize(data);
data = utils.filterFormData(data)
data = utils.serialize(utils.filterFormData(data));

data['form_key'] = window.FORM_KEY;

Expand Down
14 changes: 13 additions & 1 deletion app/code/Magento/Ui/view/base/web/js/form/element/multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ define([
return Select.extend({
defaults: {
size: 5,
elementTmpl: 'ui/form/element/multiselect'
elementTmpl: 'ui/form/element/multiselect',
listens: {
value: 'setDifferedFromDefault setPrepareToSendData'
}
},

/**
Expand All @@ -38,6 +41,15 @@ define([
return _.isString(value) ? value.split(',') : value;
},

setPrepareToSendData: function (data) {

if (!data.length) {
data = '';
}

this.source.set(this.dataScope + '-prepared-for-send', data);
},

/**
* @inheritdoc
*/
Expand Down
16 changes: 16 additions & 0 deletions lib/web/mage/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ define([
return formData;
},

filterFormData: function (data, sufix, separator) {
sufix = sufix || 'prepared-for-send';
separator = separator || '-';

_.each(data, function (value, key) {
if (_.isObject(value) && !value.length) {
this.filterFormData(value, sufix, separator)
} else if (_.isString(key) && ~key.indexOf(sufix)) {
data[key.split(separator)[0]] = value;
delete data[key];
}
}, this);

return data;
},

/**
* Converts PHP IntlFormatter format to moment format.
*
Expand Down

0 comments on commit 85db4c8

Please sign in to comment.