kopia lustrzana https://github.com/shoelace-style/shoelace
prevent form submission
rodzic
2f8852245e
commit
6446bb1013
|
@ -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();
|
||||
|
|
Ładowanie…
Reference in New Issue