Magento – Magento 2.1: Product date validation prevents saving “Make sure the To Date is later than or the same as the From Date.”

datemagento-2.1validation

Version: Magento 2.1

I cannot update or save a product due to this validation message:

Make sure the To Date is later than or the same as the From Date.

I assume it is referring to "Set Product as New From" but it could also be " Schedule Design Update From"

I have tried many combinations of dates including no dates selected but always get the same result. This is not effecting other products.

I've traced the error message to this function in "vendor/magento/module-catalog/Model/Attribute/Backend/Startdate.php".

If I remove the content of the function and just return true the product can be saved but as soon as the function is replaced the error returns blocking any updates again

/**
 * Product from date attribute validate function.
 * In case invalid data throws exception.
 *
 * @param \Magento\Framework\DataObject $object
 * @throws \Magento\Eav\Model\Entity\Attribute\Exception
 * @return bool
 */
public function validate($object)
{


    $attr = $this->getAttribute();
    $maxDate = $attr->getMaxValue();
    $startDate = $this->_getValueForSave($object);
    if ($startDate === false) {
        return true;
    }

    if ($maxDate) {
        $date = $this->_date;
        $value = $date->timestamp($startDate);
        $maxValue = $date->timestamp($maxDate);

        if ($value > $maxValue) {
            $message = __('Make sure the To Date is later than or the same as the From Date.');
            $eavExc = new \Magento\Eav\Model\Entity\Attribute\Exception($message);
            $eavExc->setAttributeCode($attr->getName());
            throw $eavExc;
        }
    }
    return true;
}

I cannot be more specific about how to replicate this issue or why it's happening to this product. The obvious work around is to delete the product and re-enter it but I'd like to see if there's a reason this is happening.

Best Answer

We were facing the similar issue with multi-store products.

We then tried to save the product at default level without any changes, and no error was observed. We then tried the same at different store levels and everything worked well and we were able to save the changes.

We are yet to find the root cause of this issue, but our assumption is that some data for the product was not saved at default level, due to which the issue was coming at store level.

If you are still facing this issue, you can try these steps.

Related Topic