mirror of
https://github.com/shoelace-style/webawesome.git
synced 2026-01-12 04:09:12 +00:00
prevent form submission
This commit is contained in:
@@ -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>
|
||||
```
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user