1

I have this date field in a form (a ui component) and i'm tryig to make it "required field", I tried with <item name="required-entry" xsi:type="boolean">true</item> but it didn't work. And also tried with a custom validation but also not working, any ideas?

Date field in the form

        <field name="date_of_birth" formElement="date">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">Date of birth</item>
                </item>     
                <item name="validation" xsi:type="array">
                    <!-- <item name="required-entry" xsi:type="boolean">true</item>
                    <item name="validate-date" xsi:type="boolean">true</item> -->
                    <item name="date-required-custom" xsi:type="boolean">true</item>
                </item>    
            </argument>
            <settings>
                <dataType>timestamp</dataType>
                <label translate="true">Date of birth</label>
                <visible>true</visible>
                <dataScope>date_of_birth</dataScope>
            </settings>
        </field> 

Custom validation (I currently have more rules and they work)

require([
    'Magento_Ui/js/lib/validation/validator',
    'jquery'
], function (validator, $) {
    validator.addRule(
        'date-required-custom',
        function (value) {
            if (!(value == null || value === 'undefined')) {
                return false;
            };
        }
        ,$.mage.__('Field required.')
    );
});

2 Answers 2

1

You can replace below code for required date field:

<field name="date_of_birth">
    <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">Date of birth</item>
            <item name="formElement" xsi:type="string">date</item>
            <item name="source" xsi:type="string">page</item>
            <item name="sortOrder" xsi:type="number">21</item>
            <item name="dataScope" xsi:type="string">date_of_birth</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</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">true</item>
            </item>
            <item name="storeTimeZone" xsi:type="string">string</item>
        </item>
    </argument>
</field>

And for your custom validation or another validation, you can see the below link, https://magento.stackexchange.com/a/363768/110348

1

Please try this, this will work :

    <field name="display_from" sortOrder="60" formElement="date">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Display From</item>
                <item name="source" xsi:type="string">test</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</item>
                    <item name="showsTime" xsi:type="boolean">true</item>
                    <item name="minDate" xsi:type="string">new Date()</item>
                </item>
            </item>
        </argument>
        <settings>
            <validation>
                <rule name="required-entry" xsi:type="boolean">true</rule>
            </validation>
            <dataType>timestamp</dataType>
            <dataScope>display_from</dataScope>
            <label translate="true">From</label>
        </settings>
    </field>

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.