Magento 2.2.3 UI Component – Save Date and MultiSelect Field Value

custom-fieldmagento2.2.3uicomponent

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.

Best Answer

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