0

I have added some fields in the Catalog Price rule Module. when I save the form data it throws an error "Unable to save rule 5" . do I need to write a custom model for this or would it save automatically?

UI Form:

<?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="name" formElement="input">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">catalog_rule</item>
            </item>
        </argument>
        <settings>
            <validation>
                <rule name="required-entry" xsi:type="boolean">true</rule>
            </validation>
            <dataType>text</dataType>
            <label translate="true">Sale Name</label>
            <visible>true</visible>
            <dataScope>name</dataScope>
        </settings>
    </field>
</fieldset>
<fieldset name="general">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="label" xsi:type="string" translate="true">Aditional Options</item>
            <item name="sortOrder" xsi:type="number">100</item>
        </item>
    </argument>
<fieldset name="banners">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="label" xsi:type="string" translate="true">Top Banner On Product Pager</item>
            <item name="sortOrder" xsi:type="number">100</item>
        </item>
    </argument>
    <field name="product_top_banner">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">string</item>
                <item name="source" xsi:type="string">catalog_rule</item>
                <item name="label" xsi:type="string" translate="true">Upload</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="formElement" xsi:type="string">fileUploader</item>
                <item name="sortOrder" xsi:type="number">50</item>
                <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
                <item name="previewTmpl" xsi:type="string">PME_FlashSales/image-preview</item>
                <item name="required" xsi:type="boolean">false</item>
                <item name="uploaderConfig" xsi:type="array">
                    <item name="url" xsi:type="url" path="flash_sales/promo/banner/"/>
                </item>
            </item>
        </argument>
    </field>
    <field name="product_top_banner_alt">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Alt Text:</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">catalog_rule</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">false</item>
                </item>
            </item>
        </argument>
    </field>
    <field name="product_top_banner_link">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Link:</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">catalog_rule</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">false</item>
                </item>
            </item>
        </argument>
    </field>
</fieldset>
    <!--After Product-->
    <fieldset name="after_banners">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="label" xsi:type="string" translate="true">After Product Description Banner</item>
                <item name="sortOrder" xsi:type="number">100</item>
            </item>
        </argument>
        <field name="product_after_banner">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="source" xsi:type="string">catalog_rule</item>
                    <item name="label" xsi:type="string" translate="true">Upload</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="formElement" xsi:type="string">fileUploader</item>
                    <item name="sortOrder" xsi:type="number">50</item>
                    <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
                    <item name="previewTmpl" xsi:type="string">PME_FlashSales/image-preview</item>
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="uploaderConfig" xsi:type="array">
                        <item name="url" xsi:type="url" path="flash_sales/promo/productbanner/"/>
                    </item>
                </item>
            </argument>
        </field>
        <field name="product_after_banner_alt">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Alt Text:</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">catalog_rule</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="product_after_banner_link">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Link:</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">catalog_rule</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                </item>
            </argument>
        </field>
    </fieldset>
<!--Label-->
    <fieldset name="labels">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="label" xsi:type="string" translate="true">Labels</item>
                <item name="sortOrder" xsi:type="number">150</item>
            </item>
        </argument>
        <field name="product_labels">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="source" xsi:type="string">product_top_banner</item>
                    <item name="label" xsi:type="string" translate="true">Upload</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="formElement" xsi:type="string">fileUploader</item>
                    <item name="sortOrder" xsi:type="number">50</item>
                    <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
                    <item name="previewTmpl" xsi:type="string">PME_FlashSales/image-preview</item>
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="uploaderConfig" xsi:type="array">
                        <item name="url" xsi:type="url" path="flash_sales/promo/label/"/>
                    </item>
                </item>
            </argument>
        </field>
        <field name="product_labels_alt">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Alt Text:</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">product_labels_alt</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                </item>
            </argument>
        </field>
    </fieldset>
</fieldset>

controller:

namespace Custom\FlashSales\Controller\Adminhtml\Promo\Catalog;

use Magento\Backend\App\Action\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\Filter\Date;
use Magento\Framework\App\Request\DataPersistorInterface;

class Save extends \Magento\CatalogRule\Controller\Adminhtml\Promo\Catalog\Save
{
/**
 * @var DataPersistorInterface
 */
protected $dataPersistor;

/**
 * @param Context $context
 * @param Registry $coreRegistry
 * @param Date $dateFilter
 * @param DataPersistorInterface $dataPersistor
 */
public function __construct(
    Context $context,
    Registry $coreRegistry,
    Date $dateFilter,
    DataPersistorInterface $dataPersistor
) {
    $this->dataPersistor = $dataPersistor;
        parent::__construct($context, $coreRegistry, $dateFilter, $dataPersistor);
}

 function execute()
{
    if ($this->getRequest()->getPostValue()) {

        /** @var \Magento\CatalogRule\Api\CatalogRuleRepositoryInterface $ruleRepository */
        $ruleRepository = $this->_objectManager->get(
            \Magento\CatalogRule\Api\CatalogRuleRepositoryInterface::class
        );

        /** @var \Magento\CatalogRule\Model\Rule $model */
        $model = $this->_objectManager->create(\Magento\CatalogRule\Model\Rule::class);
//            echo json_encode($model);exit;

        try {
            $data = $this->getRequest()->getPostValue();
//                echo "<pre>";
//                print_r($data);
//                echo "</pre>";
 //                exit;
            $this->_eventManager->dispatch(
                'adminhtml_controller_catalogrule_prepare_save',
                ['request' => $this->getRequest()]
            );

            $id = $this->getRequest()->getParam('rule_id');
            if ($id) {
                $model = $ruleRepository->get($id);
            }

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

            if (isset($data['rule'])) {
                $data['conditions'] = $data['rule']['conditions'];
                unset($data['rule']);
            }

            $model->loadPost($data);

            $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setPageData($data);
            $this->dataPersistor->set('catalog_rule', $data);
//                    print_r($data);exit;
            $ruleRepository->save($model);

            $this->messageManager->addSuccessMessage(__('You saved the rule.'));
            $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setPageData(false);
            $this->dataPersistor->clear('catalog_rule');

            if ($this->getRequest()->getParam('auto_apply')) {
                $this->getRequest()->setParam('rule_id', $model->getId());
                $this->_forward('applyRules');
            } else {
                if ($model->isRuleBehaviorChanged()) {
                    $this->_objectManager
                        ->create(\Magento\CatalogRule\Model\Flag::class)
                        ->loadSelf()
                        ->setState(1)
                        ->save();
                }
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('catalog_rule/*/edit', ['id' => $model->getId()]);
                    return;
                }
                $this->_redirect('catalog_rule/*/');
            }
            return;
        } catch (LocalizedException $e) {
            $this->messageManager->addErrorMessage($e->getMessage());
        } 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->dataPersistor->set('catalog_rule', $data);
            $this->_redirect('catalog_rule/*/edit', ['id' => $this->getRequest()->getParam('rule_id')]);
            return;
        }
    }
    $this->_redirect('catalog_rule/*/');
}
}
  • Can you please add your code? – Abhishek Panchal Jan 3 '19 at 19:06
  • code of controller or UI component? – sudo55 Jan 3 '19 at 19:11
  • can you please add both the files? – Abhishek Panchal Jan 3 '19 at 19:11
  • added the complete code. – sudo55 Jan 3 '19 at 19:24
  • You didnt define _objectManager – Abhishek Panchal Jan 3 '19 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.