import { Controller } from "stimulus"; export default class extends Controller { static targets = ["invalid", "submitting"]; // @todo Stimulus >1 get submittingIdValue() { return this.element.dataset?.formValidationSubmittingIdValue; } // @todo Stimulus >1 get invalidIdValue() { return this.element.dataset?.formValidationInvalidIdValue; } 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) { event?.preventDefault(); if (this.element.reportValidity()) { this.element.classList.remove("was-validated"); if (!this.element.hasAttribute("hx-post")) this.element.submit(); window.dispatchEvent(new CustomEvent("notification:show", { detail: { id: this.submittingIdValue } })); } else { event?.stopPropagation(); this.element.classList.add("was-validated"); window.dispatchEvent(new CustomEvent("notification:show", { detail: { id: this.invalidIdValue } })); } } }