3
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="rule_information" sortOrder="10">
        <field name="from_date" formElement="date">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="options" xsi:type="array">
                        <item name="dateFormat" xsi:type="string">yyyy-MM-dd</item>
                        <item name="timeFormat" xsi:type="string">HH:mm:ss</item>
                        <item name="showsTime" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="to_date" formElement="date">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="options" xsi:type="array">
                        <item name="dateFormat" xsi:type="string">yyyy-MM-dd</item>
                        <item name="timeFormat" xsi:type="string">HH:mm:ss</item>
                        <item name="showsTime" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

This is my sales_rule_form.xml i changed dB column as VARCHAR/datetime, its showing invalid date, and how can i save it in db.

1 Answer 1

3

This is a bug. Try following way for the fix:

app/code/SR/MagentoCommunity/etc/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Ui\Component\Form\Element\DataType\Date">
        <plugin name="SR_MagentoCommunity::allow_format_from_config"
                type="SR\MagentoCommunity\Plugin\Ui\Component\Form\Element\DataType\Date" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/Ui/Component/Form/Element/DataType/Date.ph

code>
<?php
namespace SR\MagentoCommunity\Plugin\Ui\Component\Form\Element\DataType;

class Date
{
    public function aroundPrepare(
        \Magento\Ui\Component\Form\Element\DataType\Date $subject,
        \Closure $proceed
    ) {
        $configOrig = $subject->getData('config');
        $proceed();
        $config = $subject->getData('config');
        if (isset($configOrig['options']) && isset($configOrig['options']['dateFormat'])) {
            $config['options']['dateFormat'] = $configOrig['options']['dateFormat'];
        }

        if (isset($configOrig['options']) && isset($configOrig['options']['timeFormat'])) {
            $config['options']['timeFormat'] = $configOrig['options']['timeFormat'];
        }

        $subject->setData('config', $config);
    }
}

[Update]

app/code/SR/MagentoCommunity/etc/adminhtml/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Save" type="SR\MagentoCommunity\Controller\Adminhtml\Promo\Quote\Save"/>
</config>

app/code/SR/MagentoCommunity/Controller/Adminhtml/Promo/Quote/Save.php


<?php
namespace SR\MagentoCommunity\Controller\Adminhtml\Promo\Quote;

class Save extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Save
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime\Filter\DateTime
     */
    private $dateTimeFilter;

    /**
     * Save constructor.
     *
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\Registry $coreRegistry
     * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
     * @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
     * @param \Magento\Framework\Stdlib\DateTime\Filter\DateTime $dateTimeFilter
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
        \Magento\Framework\Stdlib\DateTime\Filter\DateTime $dateTimeFilter
    ) {
        parent::__construct($context, $coreRegistry, $fileFactory, $dateFilter);
        $this->dateTimeFilter = $dateTimeFilter;
    }

    /**
     * Promo quote save action
     *
     * @return void
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function execute()
    {
        if ($this->getRequest()->getPostValue()) {
            try {
                /** @var $model \Magento\SalesRule\Model\Rule */
                $model = $this->_objectManager->create(\Magento\SalesRule\Model\Rule::class);
                $this->_eventManager->dispatch(
                    'adminhtml_controller_salesrule_prepare_save',
                    ['request' => $this->getRequest()]
                );
                $data = $this->getRequest()->getPostValue();

                $filterValues = ['from_date' => $this->dateTimeFilter];
                if ($this->getRequest()->getParam('to_date')) {
                    $filterValues['to_date'] = $this->dateTimeFilter;
                }
                $inputFilter = new \Zend_Filter_Input(
                    $filterValues,
                    [],
                    $data
                );
                $data = $inputFilter->getUnescaped();

                $id = $this->getRequest()->getParam('rule_id');
                if ($id) {
                    $model->load($id);
                    if ($id != $model->getId()) {
                        throw new \Magento\Framework\Exception\LocalizedException(__('The wrong rule is specified.'));
                    }
                }

                $session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);

                $validateResult = $model->validateData(new \Magento\Framework\DataObject($data));
                if ($validateResult !== true) {
                    foreach ($validateResult as $errorMessage) {
                        $this->messageManager->addErrorMessage($errorMessage);
                    }
                    $session->setPageData($data);
                    $this->_redirect('sales_rule/*/edit', ['id' => $model->getId()]);
                    return;
                }

                if (isset(
                        $data['simple_action']
                    ) && $data['simple_action'] == 'by_percent' && isset(
                        $data['discount_amount']
                    )
                ) {
                    $data['discount_amount'] = min(100, $data['discount_amount']);
                }
                if (isset($data['rule']['conditions'])) {
                    $data['conditions'] = $data['rule']['conditions'];
                }
                if (isset($data['rule']['actions'])) {
                    $data['actions'] = $data['rule']['actions'];
                }
                unset($data['rule']);
                $model->loadPost($data);

                $useAutoGeneration = (int)(
                    !empty($data['use_auto_generation']) && $data['use_auto_generation'] !== 'false'
                );
                $model->setUseAutoGeneration($useAutoGeneration);

                $session->setPageData($model->getData());

                $model->save();
                $this->messageManager->addSuccessMessage(__('You saved the rule.'));
                $session->setPageData(false);
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('sales_rule/*/edit', ['id' => $model->getId()]);
                    return;
                }
                $this->_redirect('sales_rule/*/');
                return;
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addErrorMessage($e->getMessage());
                $id = (int)$this->getRequest()->getParam('rule_id');
                if (!empty($id)) {
                    $this->_redirect('sales_rule/*/edit', ['id' => $id]);
                } else {
                    $this->_redirect('sales_rule/*/new');
                }
                return;
            } catch (\Exception $e) {
                $this->messageManager->addErrorMessage(
                    __('Something went wrong while saving the rule data. Please review the error log.')
                );
                $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
                $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setPageData($data);
                $this->_redirect('sales_rule/*/edit', ['id' => $this->getRequest()->getParam('rule_id')]);
                return;
            }
        }
        $this->_redirect('sales_rule/*/');
    }
}
12
  • Sohel Rana, Thank you for your support. Here it fixed validation part. but its not saving it giving error Something went wrong while saving the rule data. Please review the error log. but no error in console & Network. Feb 18, 2019 at 5:14
  • Can you show me the error from the log?
    – Sohel Rana
    Feb 18, 2019 at 5:16
  • "Navigated to 127.0.0.1/Magento1/admin/sales_rule/promo_quote/edit/key/… jquery-migrate.js:21 JQMIGRATE: Logging is active".. No error here Feb 18, 2019 at 5:18
  • This is not what I am asking. Find log from system.log or exception.log or reports?
    – Sohel Rana
    Feb 18, 2019 at 5:20
  • [2019-02-18 05:23:13] main.CRITICAL: Invalid input date format '2019-02-26T05:21:00.000Z' {"exception":"[object] (Exception(code: 0): Invalid input date format '2019-02-26T05:21:00.000Z' at C:\\xampp\\htdocs\\Magento1\\vendor\\magento\\framework\\Stdlib\\DateTime\\Filter\\Date.php:69)"} [] Exception log Feb 18, 2019 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.