2024-07-20 18:21:03 +00:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
2024-06-19 16:12:54 +00:00
|
|
|
|
|
|
|
export default class extends Controller {
|
2024-06-19 17:28:53 +00:00
|
|
|
static targets = ["invalid", "submitting"];
|
|
|
|
|
2024-06-19 21:19:07 +00:00
|
|
|
// @todo Stimulus >1
|
|
|
|
get submittingIdValue() {
|
|
|
|
return this.element.dataset?.formValidationSubmittingIdValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo Stimulus >1
|
|
|
|
get invalidIdValue() {
|
|
|
|
return this.element.dataset?.formValidationInvalidIdValue;
|
|
|
|
}
|
|
|
|
|
2024-06-19 16:12:54 +00:00
|
|
|
connect() {
|
|
|
|
this.element.setAttribute("novalidate", true);
|
|
|
|
|
|
|
|
for (const input of this.element.elements) {
|
|
|
|
if (input.type === "button" || input.type === "submit") continue;
|
|
|
|
|
|
|
|
if (input.dataset.action) {
|
|
|
|
input.dataset.action = `${input.dataset.action} htmx:validation:validate->form-validation#submit`;
|
|
|
|
} else {
|
|
|
|
input.dataset.action = "htmx:validation:validate->form-validation#submit";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
submit(event = undefined) {
|
2024-06-25 17:24:12 +00:00
|
|
|
if (this.submitting) return;
|
|
|
|
|
|
|
|
this.submitting = true;
|
|
|
|
|
2024-06-19 16:12:54 +00:00
|
|
|
event?.preventDefault();
|
|
|
|
|
|
|
|
if (this.element.reportValidity()) {
|
|
|
|
this.element.classList.remove("was-validated");
|
2024-06-19 17:28:53 +00:00
|
|
|
|
2024-06-25 17:24:12 +00:00
|
|
|
if (!this.element.getAttributeNames().some(x => x.startsWith("hx-"))) this.element.submit();
|
2024-06-19 17:28:53 +00:00
|
|
|
|
2024-07-04 19:51:08 +00:00
|
|
|
window.dispatchEvent(new CustomEvent("notification:show", { detail: { value: this.submittingIdValue } }));
|
2024-06-19 21:19:07 +00:00
|
|
|
} else {
|
|
|
|
event?.stopPropagation();
|
2024-06-19 17:28:53 +00:00
|
|
|
|
2024-06-19 21:19:07 +00:00
|
|
|
this.element.classList.add("was-validated");
|
2024-06-19 17:28:53 +00:00
|
|
|
|
2024-07-04 19:51:08 +00:00
|
|
|
window.dispatchEvent(new CustomEvent("notification:show", { detail: { value: this.invalidIdValue } }));
|
2024-06-19 16:12:54 +00:00
|
|
|
}
|
2024-06-25 17:24:12 +00:00
|
|
|
|
|
|
|
this.submitting = false;
|
2024-06-19 16:12:54 +00:00
|
|
|
}
|
|
|
|
}
|