2

This is my UI component form

 <field name="test[start_date]">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="label" xsi:type="string" translate="true">text</item>
                    <item name="formElement" xsi:type="string">date</item>
                    <item name="source" xsi:type="string">sales_rule</item>
                    <item name="sortOrder" xsi:type="number">33</item>
                    <item name="dataScope" xsi:type="string">test[start_date]</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
        </argument>
    </field>

I want to load default value for date. I added a plugin for

Magento\SalesRule\Model\Rule\Metadata\ValueProvider

this is my code

public function aroundGetMetadataValues(
        SalesRuleValueProvider $subject,
        \Closure $proceed,
        Rule $rule
    ) {
        $result = $proceed($rule);
        $actions = &$result['actions']['children']['simple_action']['arguments']['data']['config']['options'];

        $result['actions']['children']['test[start_date]']['arguments']['data']['config']['value']
            = '2014-09-01';
        return $result;
    }

All other input field are working very well. I was able to pre populate it with a value except date. I tried all date format available, Any help on this

2

you can achieve this functionality below code.

UI component form in xml file like custom_form.xml:

<field name="your_field_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="class" xsi:type="string">{Vendorname}\{Modulename}\Ui\Component\Form\Element\Input</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">End Date</item>
                    <item name="formElement" xsi:type="string">date</item>
                    <item name="source" xsi:type="string">customer</item>
                    <item name="sortOrder" xsi:type="number">89</item>                  
                   <item name="dataScope" xsi:type="string">your_field_id</item>
                   <item name="notice" xsi:type="string" translate="true">Enter the Account Removed Date of Customer(mm/dd/yyyy)</item>

                </item>
            </argument>
        </field>  

Create file In {Vendorname}\{Modulename}\Ui\Component\Form\Element\Input.php

And add the logic in element Input.php file

<?php

    namespace {Vendorname}\{Modulename}\Ui\Component\Form\Element;

    class Input extends \Magento\Ui\Component\Form\Element\Input
    {
        /**
         * Prepare component configuration
         *
         * @return void
         */
        public function prepare()
        {
            parent::prepare();

            $config = $this->getData('config');

            if(isset($config['dataScope']) && $config['dataScope']=='your_field_id'){
                $config['default']= date('Y-m-d', strtotime('+3 years'));
                $this->setData('config', (array)$config);
            }
        }
    }

Please check and let know.

  • Thanks alot @ramki, I really appreciate. It works perfectly – Soladogun Ilyas Ademuyiwa Aug 2 '18 at 6:40
  • @soladogunilyasAdemuyiwa you're welcome :) – Ramki Nov 29 '18 at 9:11
0

You can try converting the date string to a date object like this:

$dateValue = '2011-03-05'
$value = date('Y-m-d', strtotime($dateValue));

And then add it as the value.

Not sure if it works.

Your Answer

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

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