0

I've been facing issue with saving date field value.

I've added custom date field in sales_rule form , Field is added but can't able to save its value to DB.

Previously i added text field and its working fine but I am having issues for date and MultiSelect input.

Vendor/Module/adminhtml/ui_component/sales_rule_form.xml

<field name="end_date" formElement="date">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">sales_rule</item>
            </item>
        </argument>
        <settings>
            <validation>
                <rule name="validate-date" xsi:type="boolean">true</rule>
            </validation>
            <dataType>text</dataType>
            <label translate="true">End Date</label>
            <visible>true</visible>
            <dataScope>end_date</dataScope>
        </settings>
    </field>

    <field name="brand_ids" formElement="multiselect">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">sales_rule</item>
            </item>
        </argument>
        <settings>
            <validation>
                <rule name="validate-brands" xsi:type="boolean">true</rule>
            </validation>
            <dataType>number</dataType>
            <label translate="true">Brand List</label>
            <dataScope>brand_ids</dataScope>
        </settings>
         <formElements>
        <multiselect>
            <settings>
                <options class="Vivek\ShopAsGroup\Block\SalesRule\BrandList"/>
            </settings>
        </multiselect>
</formElements>
    </field>

DB

What is missing ? Any suggestion would be really helpful to me.

Thanks in Advance.

6
  • use dataSource tag in your file or not. Commented Apr 4, 2018 at 5:15
  • No, I didn't use that. Can you please give me an example.
    – anonymous
    Commented Apr 4, 2018 at 5:16
  • Are you using magento 2.2.3 ? Commented Apr 10, 2018 at 9:09
  • @EmiproTechnologiesPvt.Ltd., Yes
    – anonymous
    Commented Apr 10, 2018 at 9:43
  • In which table you want to save these two fields value in salesrule table or in your own table ? Commented Apr 10, 2018 at 11:12

3 Answers 3

5
+25

This is completely untested but here goes.
As I can see for the other date type fields, the value needs some processing before saving it into the db: https://github.com/magento/magento2/blob/2.2/app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote/Save.php#L30

Maybe you need to do the same for your date fields as well.
You can use this event adminhtml_controller_salesrule_prepare_save that receives as parameter the instance of the request, so you can get the post data and modify it.

As for multiselects, their selected values are sent as an array and you need to convert it to a string before saving it in the db.
You can use the same event as above to do that.

something like:

$request = $observer->getEvent()->getRequest();
$brandIds = $request->getParam('brand_ids');
$brandIds = implode(',', $brandIds);
$request->setParam('brand_ids', $brandIds);

If this works, it should solve the other issue you have: Magento 2 : Notice Error - Array to String conversion

3
  • I tried but it's not working. If you have any other solution than you can tell me. Same exception with above Array to string conversion.
    – anonymous
    Commented Apr 13, 2018 at 5:12
  • 1
    no other ideas. It worked like this for me but in a custom module where I had control over the controller. Here you don't have access to directly modify the controller.
    – Marius
    Commented Apr 13, 2018 at 6:39
  • Above mentioned link reflect 404 error and correct link was github.com/magento/magento2/blob/2.3-develop/app/code/Magento/… Commented Aug 12, 2019 at 13:17
1

I am trying to modifying Marius answer, He made me close to my issue.

I tried below code and it helps me to resolve my issue.

I used event adminhtml_controller_salesrule_prepare_save.

    $data = $observer->getRequest()->getPostValue();
    //print_r($data); exit;
    $request = $observer->getEvent()->getRequest();

    $brands = implode(',',$data['brands']);

    $startDate = $this->timeZoneInterface->date($data['start_date'])->format('Y-m-d'); 
    $endDate = $this->timeZoneInterface->date($data['end_date'])->format('Y-m-d');


    $request->setPostValue('brands',$brands);
    $request->setPostValue('start_date',$startDate);
    $request->setPostValue('end_date',$endDate);

Now Value has been saved to DB without any exception OR Notice.

Hope it will help others.

0
0

Use this code and modify as per your requirements.

<?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">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">sales_rule_form.sales_rule_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Main title</item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
    </argument>
    <settings>
        <buttons>
            <button name="delete" class="Vivek\ShopAsGroup\Block\Adminhtml\Promo\Quote\Edit\DeleteButton"/>
            <button name="save_and_continue" class="Vivek\ShopAsGroup\Block\Adminhtml\Promo\Quote\Edit\SaveAndContinueButton"/>
            <button name="reset" class="Vivek\ShopAsGroup\Block\Adminhtml\Promo\Quote\Edit\ResetButton"/>
            <button name="save" class="Vivek\ShopAsGroup\Block\Adminhtml\Promo\Quote\Edit\SaveButton"/>
            <button name="back">
                <url path="*/*/"/>
                <class>back</class>
                <label translate="true">Back</label>
            </button>
        </buttons>
        <namespace>sales_rule_form</namespace>
        <dataScope>data</dataScope>
        <deps>
            <dep>sales_rule_form.sales_rule_form_data_source</dep>
        </deps>
    </settings>
    <dataSource name="sales_rule_form_data_source">
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
            </item>
        </argument>
        <settings>
            <submitUrl path="sales_rule/promo_quote/save"/>
        </settings>
        <dataProvider class="Vivek\ShopAsGroup\Model\Rule\DataProvider" name="sales_rule_form_data_source">
            <settings>
                <requestFieldName>id</requestFieldName>
                <primaryFieldName>rule_id</primaryFieldName>
            </settings>
        </dataProvider>
    </dataSource>
        <fieldset name="rule_information" sortOrder="10">
        <settings>
            <collapsible>true</collapsible>
            <opened>true</opened>
            <label translate="true">Tab title</label>
        </settings>
        <field name="name" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">sales_rule</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
                <dataType>text</dataType>
                <label translate="true">Rule Name</label>
                <visible>true</visible>
                <dataScope>name</dataScope>
            </settings>
        </field>
    </fieldset>
</form>

I think you not use datasource thats why you can not save date.

I hope this will help you.

7
  • So, you mean i need to override Magento\SalesRule\Model\Rule\Dataprovider Class ? To save date and multiselct fields value ?!
    – anonymous
    Commented Apr 4, 2018 at 5:27
  • No. get reference from here. it is helpful for you. Commented Apr 4, 2018 at 5:28
  • Use this code and modify as per your requirements. i hope its work for you. Commented Apr 4, 2018 at 5:29
  • I don't understand, What you exactly mean, I've add datasource tag to my existing xml but nothing happens, I checked vendor code dataprovider classis just load all data from form.
    – anonymous
    Commented Apr 4, 2018 at 5:30
  • In this code <submitUrl path="sales_rule/promo_quote/save"/> this is use for to save your form data. You use this ? Commented Apr 4, 2018 at 5:39

Your Answer

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

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