Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top