Skip to content
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

Unable to unselect all multiselect values #7687

Closed
Vinai opened this issue Dec 5, 2016 · 35 comments
Closed

Unable to unselect all multiselect values #7687

Vinai opened this issue Dec 5, 2016 · 35 comments
Labels
bug report Component: Catalog Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development

Comments

@Vinai
Copy link
Contributor

Vinai commented Dec 5, 2016

Magento version: 2.1.2

It is not possible to unselect and save all multiselect attribute values for a product.
After the save completes the state of the product attribute before the product was saved is unchanged.
It seems the problem is that the JS does not submit multiselect form elements with no selected values, at least on the server side completely unselected multiselect attribtues are not part of the $_POST['product'] array.

The exact same issue existed for years on M1 but was finally fixed there. It seems like this issue was introduced again for M2.

Preconditions

None special.

Steps to reproduce

  1. Create a custom multiselect attribute. Ot just install the sample data (e.g. the issue can be reproduced using the features_bag or strap_bags attributes).
  2. Open the product in the admin interface.
  3. Select one or more values of the multiselect attribtue.
  4. Save
  5. Unselect all values of the multiselect attribute
  6. Save again

Expected result

The product attribute should have no value for the product.

Actual result

The previously selected product attributes still are selected.

@Vinai
Copy link
Contributor Author

Vinai commented Dec 5, 2016

@fooman gave the input that probably the can_be_empty property has to be set on the \Magento\Framework\Data\Form\Element\Multiselect instance.
For system configuration multiselect that can be done by adding the <can_be_empty> node to the system.xml file, but in case of EAV attributes I see no obvious way to do so since they are DB based.

@Vinai
Copy link
Contributor Author

Vinai commented Dec 5, 2016

On a side note, the method \Magento\Framework\Data\Form\Element\Multiselect::getElementHtml() adds the CSS class admin__control-multiselect (amongst others). Should the class not also be usable on the store front?

@Vinai
Copy link
Contributor Author

Vinai commented Dec 5, 2016

More info in case it's useful: the property can_be_empty would be set in \Magento\Backend\Block\Widget\Form::_applyTypeSpecificConfig() but that method is never called. There are 3 classes that would trigger the flow of control to enter that method, but none of three classes are used on the edit product form. Is this M1 legacy code that wasn't deleted?

\Magento\Catalog\Block\Adminhtml\Product\Edit\Action\Attribute\Tab\Attributes::_prepareForm()
\Magento\Catalog\Block\Adminhtml\Product\Attribute\NewAttribute\Product\Attributes::_prepareForm()
\Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Attributes::_prepareForm()

@ivanweiler
Copy link

It seems that same problem exists with configuration multiselects. For example
Stores>Configuration>General>Top destinations - not possible to unselect all and save in 2.1.2.

@antboiko
Copy link

Hi @Vinai , thanks for reporting this issue. We have internal ticket MAGETWO-57607 to track this issue on our side.

Best,
Anton.

@antboiko antboiko added 2.1.x Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Component: Catalog labels Dec 15, 2016
@pravalitera
Copy link

pravalitera commented Dec 27, 2016

It happens also on custom attributes with custom sources. I have put Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend as backend_model .

When i save in backoffice without options, i have a string in the before save with the previous options selected.
When i save in backoffice with one/more options, i have an array.

@erikhansen
Copy link
Contributor

@antboiko Any update on this issue? A Magento Enterprise merchant I'm working with is experiencing this issue and they're looking for a patch. Before emailing support for a patch, I wanted to see if this was fixed in develop so that I can grab a patch?

mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
mmansoor-magento pushed a commit that referenced this issue Feb 1, 2017
Bugs
- MAGETWO-63022 [2.2] SCD does not work when multiple languages are specified.
- MAGETWO-62276 'State/Province' isn't changed to input after selecting 'United Kingdom' while filling address on checkout
- MAGETWO-63009 Jasmine test randomly fails on bamboo
- MAGETWO-54733 Unable to save product with all unchecked values for multiple select attribute #7687
- MAGETWO-59966 [SWAT][Magento Cloud] - Cache-miss when Fastly is enabled
- MAGETWO-60258 [Magento Cloud] - Product position set in the admin is not honored in the category when seen in the Frontend
@wsagen
Copy link

wsagen commented Mar 9, 2017

Any update on this?

@roseofgold
Copy link

roseofgold commented May 2, 2017

I'd like to know how to get this fixed. It's driving me nuts. (Tested on both 2.1.2 and 2.1.6)

@redelschaap
Copy link
Contributor

Bug is still present in 2.1.5.

@nkajic
Copy link
Member

nkajic commented May 26, 2017

@drobinson
Copy link

@veloraven @magento-engcom-team Do you know where this was fixed for the 2.1 branch? I see the backport commit here but this does not fix the issue for me (introduces a new issue): 7ec8952

If there's a better place for me to describe the issue please let me know.

@WilliamChongSDG
Copy link

This issue still persist in Magento ver. 2.2.2

@danielmaier42
Copy link

Still present in Magento ver. 2.2.3

@mathieu-f
Copy link

I had the same issue because of multiselect having an empty value (ie. equal to '' or 0 or null) :

<select multiple="" class="admin__control-multiselect" data-bind="
    attr: {
        name: inputName,
        id: uid,
        size: size ? size : '6',
        disabled: disabled,
        'aria-describedby': noticeId,
        placeholder: placeholder
    },
    hasFocus: focused,
    optgroup: options,
    selectedOptions: value,
    optionsValue: 'value',
    optionsText: 'label'" name="product[customer_group]" id="MEAKNUP" size="6" aria-describedby="notice-MEAKNUP">
   <option data-title="NOT LOGGED IN" value="0">NOT LOGGED IN</option> <!-- here is the devil -->
   <option data-title="General" value="1">General</option>
   <option data-title="Conseillère" value="2">Conseillère</option>
</select>

Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper\AttributeFilter prepare your attributes before saving it.

public function prepareProductAttributes(Product $product, array $productData, array $useDefaults)
{
    foreach ($productData as $attribute => $value) {
        $considerUseDefaultsAttribute = !isset($useDefaults[$attribute]) || $useDefaults[$attribute] === "1";
        if ($value === '' && $considerUseDefaultsAttribute) {
            /** @var $product Product */
            if ((bool)$product->getData($attribute) === (bool)$value) {
                unset($productData[$attribute]);
            }
        }
    }
    return $productData;
}

When a posted value is equal to empty string ('') it will compare it to the product value before saving.
The issue is that Magento cast both values in boolean. And when the previously selected product value was set to 0 (like "NOT LOGGED IN" in our example) it compares an empty string casted in boolean (= false) with a 0 casted in boolean (= false too) so the value is simply unset ... so the attribute isn't saved and there is no error.

In our specific case, we fixed it using a plugin to restore the value after the prepareProductAttributes function has been called.

I hope it will help.

@likemusic
Copy link
Contributor

Why this issue is closed?

@VladimirZaets
Copy link
Contributor

@likemusic

Why this issue is closed?

  • This issue isn't reproduced on current 2.2 and 2.3 Magento branch

@devamitbera
Copy link

devamitbera commented Jun 5, 2018

Still this issue also persist at magento 2.2.4 .

Have done with Vinai describe scenario and same issue is happening

@Vinai ,Is this issue resolved at your end?

@magento/community-engineering-team ,Can you please check this issue again

@aurelius15
Copy link

I hope this will help someone.
di.xml

<type name="\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper\AttributeFilter">
        <plugin name="Vendor_Module::prepareProductAttributes" type="\Vendor\Module\Plugin\Product\AttributeFilter"/>
    </type>

Plugin\AttributeFilter.php

use \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper\AttributeFilter as AttributeFilterHelper;

public function afterPrepareProductAttributes(AttributeFilterHelper $subject, $result, $product, $productData)
    {
        foreach ($productData as $attribute => $value) {
            $pos = strripos($attribute, '-prepared-for-send');
            if ($pos === false) {
                continue;
            }
            $attr = substr($attribute, 0, $pos);
            if (isset($result[$attr])) {
                continue;
            }
            $result[$attr] = '';
        }
        return $result;
    }

@ihor-sviziev
Copy link
Contributor

@engcom-backlog-nazar @engcom-backlog-nickolas could you please recheck if this issue still there?
People were complaining that it wasn’t fixed

@ihor-sviziev ihor-sviziev reopened this Jan 3, 2019
@ghost
Copy link

ghost commented Jan 3, 2019

@ihor-sviziev, ok i will re check this issue

@ghost ghost self-assigned this Jan 3, 2019
@magento-engcom-team
Copy link
Contributor

magento-engcom-team commented Jan 3, 2019

Hi @engcom-backlog-nazar. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • 6. Add label Issue: Confirmed once verification is complete.

  • 7. Make sure that automatic system confirms that report has been added to the backlog.

@ghost
Copy link

ghost commented Jan 3, 2019

@ihor-sviziev seems like this already fixed in 2.3.0 release

multiselect

@aurelius15
Copy link

@engcom-backlog-nazar

  1. Select some attribute items and save
  2. unselect all items this attribute and try to save

@ghost
Copy link

ghost commented Jan 3, 2019

@aurelius15 same with

unselect all items this attribute and try to save

@ihor-sviziev
Copy link
Contributor

@engcom-backlog-nazar did you checked this issue on 2.2 release line?

@ghost
Copy link

ghost commented Jan 9, 2019

@ihor-sviziev yes, same on 2.2-develop

@ghost ghost closed this as completed Jan 9, 2019
@ghost ghost reopened this Jan 9, 2019
@ghost
Copy link

ghost commented Jan 10, 2019

@ihor-sviziev can we close this issue ?

@ihor-sviziev
Copy link
Contributor

@engcom-backlog-nazar sure, if we can’t reproduce this issue

@wenpingguo
Copy link

I cannot find the fix.
Can we have a patch for the fixes?
Many thanks

@adm-sport
Copy link

i have the same issue for the 2.2.8.. Does the 2.2.x have a fix for that? we are unable to unselect all values :'(

@lano-vargas
Copy link

lano-vargas commented Sep 4, 2020

Still an issue on 2.3.5 I'm facing same issue. key pair value not set in the post when all values are deselected.
Note: Fixed multi select in system.xml with can_be_empty.

@wasimhc
Copy link

wasimhc commented May 10, 2022

It works on vendor\magento\module-payment\etc\adminhtml\system.xml
It also works on custom module too. If showInStore is used for multiselect and its 's showInStore is not specified, can_be_empty doesn't work at my end. If showInStore is removed from multiselect field in this case, can_be_empty works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Component: Catalog Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development
Projects
None yet
Development

No branches or pull requests