i'd like to know if there's a way to use the validation state of a form's fields to authorize/prevent an action to be performed.
It may be clearer with an example. I'm displaying a form with mandatory fields to be filled in order to create a record in db, and a save button to do so.
I'd like the save button action to be prevented if the static validation of the form is not ok. Is there a built-in mechanism to do so? Thanks
FormPanel
has a method isValid
that will automatically validate all the contained fields (implementing IsField
) by calling isValid
on them. If any returns false
, the form's isValid
method will also return false
.
You can use the FormPanel.isValid
call in a ClickHandler
for your form's save button and just return without doing anything if it's not valid.
@UiHandler("formPanel")
void onSave(ClickEvent event) {
if (!formPanel.isValid()) {
return;
}
saveToDb();
}