magento2: M2.1.2 : JavaScript validate-digits rule doesn't validate negative numbers, making it impossible to save a product with a negative quantity

I stumbled upon this issue when a product had a negative stock and it made it impossible to save:

screen shot 2016-11-11 at 10 44 39

It seems that the validate-digits rule doesn’t take into account negative numbers:

"validate-digits": [
    function(value) {
        return utils.isEmptyNoTrim(value) || !/[^\d]/.test(value);
    },
    $.mage.__('Please enter a valid number in this field.')
],

To match with negative numbers as well, the regex should be /[^-?\d]/. Or could it be that the validation rule should return false when digits are negative? In that case, the validate-digits-rule should be removed from the quantity box.

Or is it intentional that a product cannot be saved with a stock qty < 0? Because in that case, I would surely like to know why. I got a client who uses the backorder qty as a reference so he knows how much inventory he needs to buy.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (5 by maintainers)

Commits related to this issue

Most upvoted comments

This fix doesn’t seem to work anymore in Magento 2.1.3. Could it be that something has changed on how metadata is modified?

I’ve now created workaround that products with a qty below zero can be saved, by simple removing the validate-digits filter using a modifier, maybe this can help someone who’s running into the same issue:

/**
 * @param array $meta
 */
public function modifyMeta(array $meta)
{
    if ($path = $this->arrayManager->findPath('quantity_and_stock_status_qty', $meta, null, 'children')) {
        $this->arrayManager->remove(
            $path . '/children/qty/arguments/data/config/validation/validate-digits',
            $meta
        );
    }

    if ($path = $this->arrayManager->findPath('advanced_inventory_modal', $meta)) {
        $meta = $this->arrayManager->merge(
            $path . '/children/stock_data/children/qty/arguments/data/config',
            $meta,
            ['validation' => ['validate-digits' => false]]
        );
    }

    return $meta;
}

di.xml:

<!--
    Add a modifier to remote the validate-digits - rule from the Quantity box
-->
<virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
    <arguments>
        <argument name="modifiers" xsi:type="array">
            <item name="hf_quantity" xsi:type="array">
                <item name="class" xsi:type="string">Vendor\Module\Ui\DataProvider\Product\Form\Modifier\Quantity</item>
                <item name="sortOrder" xsi:type="number">10000</item>
            </item>
        </argument>
    </arguments>
</virtualType>

Please note the sortOrder of 10000. This is required because otherwise the advanced_inventory_modal-node is not yet available.

But the main question is: ‘is this a bug or intended behavior’? If It’s a bug I can make a pull request that removes the validate-digits-rule from the quantity boxes, but if it’s not this ticket can be closed and used as a reference on how to work around this ‘feature’.

But if this ticket gets closed, please share with us the reasoning why a product qty can not be below 0.

Well its still happening in 2.1.7 and so this ticket shouldn’t be closed until its actually posted.

@kanduvisla - For your work around, where do you implement those code changes. I need to do this ASAP, as we need to get live with a site. I’ll revert the code once an official patch has been provided.

Thanks in advance.

@kanduvisla thank you for your report. We already have internal ticket for this issue: MAGETWO-60599. I have added a link to this issue to it.