Files
openfoodnetwork/app/webpacker/controllers/paymentmethod_controller.js
Jean-Baptiste Bellet 7159cc3ff1 Handle required attribute on input for PaymentMethod controller
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.
2021-12-07 16:27:22 +00:00

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;
}
});
}
}