Is it possible to use output from JavaScript query in Custom Rule?

I have a form where each field has several rule validations. The "custom rule" option available on each field comes in handy, but with several rules becomes unreadable and unmantainable.

There is any way to use the JavaScript output in "custom rule"?

Script example:

if(field1.value.length < 3) {
return { valid: false, message: "Too short..." };
} else if (field1.value.length > 10) {
return { valid: false, message: "Too long" };
} else {
return { valid: true, message ""};
}

I know that this example can be simplified, but for the sake of the scenario I intend to illustrate, bear with me.

My goal is to achieve something like:
{{ !jsQuery.data.valid ? jsQuery.data.message : ''}}

BTW, I tried this with a transformer, but it just doesn't recognize the fields (although they are recognized everywhere else).

1 Like

hey @mascas and welcome!

transformers require brackets {{ ... }} while other queries like JS scripts don't (i.e., you just reference app components/queries straight up). also transformers usually can't execute functions of queries or component, they can basically only return values.

what i'd recommend is make a transformer like isValidMyField1:

if ({{field1.value.length}} < 3) {
    return { valid: false, message: "Too short..." };
} else if ({{field1.value.length}} > 10) {
    return { valid: false, message: "Too long" };
} else {
    return { valid: true, message: ""};
}

try that out and let me know!

1 Like

Hi @trz-justin-dev

This is it! Worked as expected!

Thanks for the support.

1 Like

@mascas awesome happy to hear!