cancel
Showing results for 
Search instead for 
Did you mean: 

Add custom js form validation based on another element?

Add custom js form validation based on another element?

Hello,

I create custom validator:

require(
    [
        'Magento_Ui/js/lib/validation/validator',
        'jquery',
        'mage/translate'
    ], function(validator, $){
        validator.addRule(
            'custom-validation',
            function (value, element) {
                if(value){
                    return true;
                }
                return false;
            }
            ,$.mage.__('Field is required.')
        );
    });

and add validation to field:

 <field name="title" sortOrder="90" formElement="input">
                            <argument name="data" xsi:type="array">
                                <item name="config" xsi:type="array">
                                    <item name="validation" xsi:type="array">
                                        <item name="custom-validation" xsi:type="boolean">true</item>
                                    </item>
                                </item>
                            </argument>
                        </field>

this code work, validation is fired but I must add another condition based on different element and I must know which element fired this validator. How can I achive this? I cannot use static JQuery ex. $('.field').val() because this fields are generated by dynamic-rows.