mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
This is done for one reason : do not submit form with required attribute on input that are actually hidden ; this is not handle correctly by browsers. This idea here is to add/remove the required attribute on each input if the form is visible or not.
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
import { Controller } from "stimulus";
|
|
export default class extends Controller {
|
|
static targets = ["paymentMethod"];
|
|
|
|
connect() {
|
|
this.selectPaymentMethod();
|
|
}
|
|
|
|
selectPaymentMethod(event = null) {
|
|
const paymentMethodContainerId = event
|
|
? event.target.dataset.paymentmethodId
|
|
: null;
|
|
Array.from(
|
|
document.getElementsByClassName("paymentmethod-container")
|
|
).forEach((e) => {
|
|
if (e.id === paymentMethodContainerId) {
|
|
e.style.display = "block";
|
|
this.addRequiredAttributeOnInputIfNeeded(e);
|
|
} else {
|
|
e.style.display = "none";
|
|
this.removeRequiredAttributeOnInput(e);
|
|
}
|
|
});
|
|
}
|
|
|
|
removeRequiredAttributeOnInput(container) {
|
|
Array.from(container.getElementsByTagName("input")).forEach((i) => {
|
|
if (i.required) {
|
|
i.dataset.required = i.required;
|
|
i.required = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
addRequiredAttributeOnInputIfNeeded(container) {
|
|
Array.from(container.getElementsByTagName("input")).forEach((i) => {
|
|
if (i.dataset.required === "true") {
|
|
i.required = true;
|
|
}
|
|
});
|
|
}
|
|
}
|