How to check if the field is visible on the form in onSubmit Catalog Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 11:38 AM
Hi Community Members!
I've encountered the problem while trying to check if the field is visible on the form.
The logic of this part of the Catalog item is: there's a field let's say "Area", it's a reference type, and when the user selects smth in there (e.g. "AB"), the second field "Division" (also reference) comes up, which is division_ab. If in Area user selects "CD", the field division_cd comes up instead. Like:
area = AB --> division_ab is visible
area = CD --> division_cd is visible
area = EF --> division_ef is visible
So depending on what users selects in Area (7 options), one of 7 corresponding Division variables will come up.
There can be a scenario when the users selects Area - AB, fills division_ab, changes the area to CD, fills the division_cd and submits the form. That's where I need your help: I need to put a logic in onSubmit Catalog Client Script that will check if the field is visible currently on the form or not, and if it's not visible - clear the value of this field.
What's worked out so far BUT only on the back-end view (meaning it's not working on the Portal view (error: "g_form.isFieldVisible is not a function")) is:
function clearFieldIfNotVisible(fieldName) {
if (!g_form.isFieldVisible(fieldName)) {
g_form.clearValue(fieldName);
}
}
function onSubmit() {
...
clearFieldIfNotVisible('division_ab');
clearFieldIfNotVisible('division_bc'); ...
..
}
g_form.isVisible() is not working, after clicking on "Submit" there's an error:
Maybe this can somehow be done with g_form.getElement()... and dot-walking to something which will tell if the field is visible on the form, or somehow else..
Thanks in advance everyone.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 05:07 PM - edited 02-09-2024 05:08 PM
Hey @Anastassia Bala ,
Instead of checking if a field is 'visible', you can use UI Policies to clear value of variables as well.
For example, when area != AB, you can set the UI Policy Action on the 'divison_ab' variable to set the 'Visible' to false and 'Clear the variable value' to true. Make sure 'Reverse if true' is set to true in the UI Policy as well.
Once you have the UI Policies, you can simply check if a value is populated for each of the divsion_xx variables.
Hope it helps, thanks!