prevent form submission

This commit is contained in:
Burton Smith
2022-07-29 12:31:29 -04:00
parent 2f8852245e
commit 6446bb1013
2 changed files with 12 additions and 1 deletions

View File

@@ -116,7 +116,7 @@ Use the `setCustomValidity()` method to set a custom validation message. This wi
// Handle form submit
form.addEventListener('submit', event => {
event.preventDefault();
// alert('All fields are valid!');
alert('All fields are valid!');
});
</script>
```

View File

@@ -61,6 +61,7 @@ export default class SlRadioGroup extends LitElement {
connectedCallback() {
super.connectedCallback();
this.preventInvalidSubmit();
}
setCustomValidity(message: string) {
@@ -103,6 +104,16 @@ export default class SlRadioGroup extends LitElement {
}
}
private preventInvalidSubmit() {
this.closest('form')?.addEventListener('submit', (e) => {
if(this.isInvalid) {
this.showNativeErrorMessage();
e.preventDefault();
e.stopImmediatePropagation();
}
})
}
private showNativeErrorMessage() {
this.input.hidden = false;
this.input.reportValidity();