I have a website with a pretty long sign up form and I’m doing it through a custom Corvid form so I can have nifty features like my multiple choice question having an “Other” option with a InputText. I wanted to check the validity of all the elements of the page automatically, and it wasn’t immediately clear so I’m writing it up.
Step one is learning that we can use $w(“TextInput”) to get all elements of that type. However, that does not return the actual items (each array element is a dictionary that includes an ID). We can do $w( “TextInput” ).map(el => el.id) to get all the IDs, or to get the actual elements:
let items = $w("TextInput").map(el => $w("#" + el.id)));
This will show an error in the editor because it doesn’t think that the TextInput item has a map function. However, this will run just fine (seems to be a Wix editor bug). You can use it like an array as normal.
Similarly, if you want all types of inputs, consider something like this:
let items = [...$w("TextInput"), ...$w("CheckboxGroup"), ...$w("Dropdown"), ...$w("TextBox")].map(el => $w("#" + el.id));
Last but not least, to check the validity, here’s the next step:
let invalidCount = items.filter(el => !el.valid).length;
console.log("You have " + invalidCount + " invalid input" + (invalidCount === 1 ? "" : "s"));