cancel
Showing results for 
Search instead for 
Did you mean: 

Disable ui component field validation rules

Disable ui component field validation rules

I have problem with disabling a specific validation rule for an existing ui component field. I've set the higlighted field to false before, but it just doesn't work. I'm looking for an upgrade-safe solution, because removing it from the core is working, but that's not the correct way.

 

<field name="to_date" formElement="date">
            <settings>
                <validation>
                    <rule name="validate-date" xsi:type="boolean">true</rule>
                </validation>
                <dataType>text</dataType>
                <label translate="true">To</label>
                <visible>true</visible>
                <dataScope>to_date</dataScope>
            </settings>
        </field>

The highlighted line:

<rule name="validate-date" xsi:type="boolean">true</rule>

 

3 REPLIES 3

Re: Disable ui component field validation rules

In your own module you can overwrite the UI component 

 

If the UI Component XML name is cms_page_form.xml the in your module you can do like below 

 

 MyNameSpace/MyMobule/view/adminhtml/ui_component/cms_page_form.xml
Suman Kar(suman.jis@gmail.com) Magento Certified Developer Plus Skype: sumanphptech Problem solved? Please give 'Kudos' and accept 'Answer as Solution'.

Re: Disable ui component field validation rules

I've used this solution first but it inherits it's parent's validation settings to:

 

<settings>
<validation>
<rule name="validate-optional-datetime" xsi:type="boolean">true</rule>
</validation>
</settings>

So I ended up using a Plugin on the date type form elements to solve this.

 

<?php
namespace TLSoft\RuleDateTime\Plugin\Ui\Component;
use Magento\Ui\Component\Form\Element\DataType\Date as _Date;

class Date
{

    public function afterPrepare(_Date $subject,$result)
    {
        $settings = $subject->getData('config');
        $name = $subject->getData('name');

        if($name=="to_date" || $name=="from_date"){
            if(array_key_exists('validate-optional-datetime',$settings['validation'])){
                unset($settings['validation']['validate-date']);
                $subject->setData('config',$settings);
            }
        }
        return $subject;
    }
}

Here's the di.xml for anybody who struggles with this:

 

<type name="Magento\Ui\Component\Form\Element\DataType\Date">
<plugin name="TLSoft_RuleDateTime_Ui_Component_Date" type="TLSoft\RuleDateTime\Plugin\Ui\Component\Date" />
</type>

I've used this to change the sales and catalog rules date fields to can also accept hours. For this I had to remove the old validation, which only accepted dates without hours and minutes in the input field. But this can also used to remove other validations too for other fields if needed (with the correct Form Element Type).

 

But if there's a cleaner solution than this plugin, feel free to correct me. I would love to see some cleaner approaches Smiley Happy.

Re: Disable ui component field validation rules

changing true to false works for me. Without using any plugins, just by overriding parent's xml file