Magento – Magento 2 UI Form datepicker, default date and strange behaviour

datepickermagento2ui-form

I created a UI Form with a datepicker field. In the XML I set required-entry to false. It keeps returning 'This is a required field'.

In the XML the definition is thus:

<field name="begin_datum">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">string</item>
            <item name="component" xsi:type="string">Blah_BlahToo/js/form/element/date</item>
            <item name="label" xsi:type="string" translate="true">Schedule From</item>
            <item name="formElement" xsi:type="string">date</item>
            <item name="source" xsi:type="string">block</item>
            <item name="sortOrder" xsi:type="number">230</item>
            <item name="dataScope" xsi:type="string">begin_datum</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">false</item>
            </item>
            <item name="options" xsi:type="array">
                <item name="dateFormat" xsi:type="string">yyyy-MM-dd</item>
                <item name="timeFormat" xsi:type="string">HH:mm:ss</item>
                <item name="showsTime" xsi:type="boolean">false</item>
            </item>
        </item>
    </argument>
</field>

My Blah_BlahToo/js/form/element/date component is this:

define([
    'Magento_Ui/js/form/element/date'
], function(Date) {
    'use strict';

    return Date.extend({
        defaults: {
            options: {
                showsDate: true,
                showsTime: false,
                required: false
            },
        }
    });
});

I thought, well let's give it a default value with

 <item name="class" xsi:type="string">Blah\BlahToo\Ui\Component\Form\Element\BeginDatum</item>

The code for this class is :

namespace Blah\BlahToo\Ui\Component\Form\Element;

class BeginDatum 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']=='begin_datum'){
            $config['default']= date('Y-m-d', strtotime('-1 month'));
            $this->setData('config', (array)$config);
        }
    }
}

Now whenever I try to pick a value, return is something like +00020201021-12-04 or an invalide date. For as before, when I did NOT preset the value with a default by that class, any returned value from the datepicker would be in the proper format.

How should I tame this wretched datepicker? Preferably I would like to do without the setting of a default and simply have Magento 2 allow a null value ( or whatever it is ) but then I keep getting that stupid "This is a required field" )

Best Answer

<item name="validation" xsi:type="array">
    <item name="required-entry" xsi:type="boolean">false</item>
</item>

This means add validation method "required-entry" and pass to this method param "false". Remove these lines, clean cache and everything will work.