Skip to content

Commit da2e6cc

Browse files
committed
feat(pat-validation): Support form elements outside the form.
1 parent 8301a6f commit da2e6cc

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/pat/validation/validation.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ class Pattern extends BasePattern {
6969
this.form.setAttribute("novalidate", "");
7070
}
7171

72+
get inputs() {
73+
// Return all inputs elements
74+
return [...this.form.elements].filter((input) =>
75+
input.matches("input[name], select[name], textarea[name]")
76+
);
77+
}
78+
79+
get disableable() {
80+
// Return all elements, which should be disabled when there are errors.
81+
return [...this.form.elements].filter((input) =>
82+
input.matches(this.options.disableSelector)
83+
);
84+
}
85+
7286
validate_all(event) {
7387
// Check all inputs.
7488
for (const input of this.inputs) {
@@ -77,13 +91,6 @@ class Pattern extends BasePattern {
7791
}
7892

7993
initialize_inputs() {
80-
this.inputs = [
81-
...this.form.querySelectorAll("input[name], select[name], textarea[name]"),
82-
];
83-
this.disabled_elements = [
84-
...this.form.querySelectorAll(this.options.disableSelector),
85-
];
86-
8794
for (const [cnt, input] of this.inputs.entries()) {
8895
// Cancelable debouncer.
8996
const debouncer = utils.debounce((e) => {
@@ -365,7 +372,7 @@ class Pattern extends BasePattern {
365372
let inputs = [input];
366373
if (all_of_group) {
367374
// Get all inputs with the same name - e.g. radio buttons, checkboxes.
368-
inputs = this.inputs.filter((it) => it.name === input.name);
375+
inputs = [...this.form.elements].filter((_input) => _input.name === input.name);
369376
}
370377
for (const it of inputs) {
371378
if (clear_state) {
@@ -378,7 +385,7 @@ class Pattern extends BasePattern {
378385

379386
// disable selector
380387
if (this.form.checkValidity()) {
381-
for (const it of this.disabled_elements) {
388+
for (const it of this.disableable) {
382389
if (it.disabled) {
383390
it.removeAttribute("disabled");
384391
it.classList.remove("disabled");
@@ -402,7 +409,7 @@ class Pattern extends BasePattern {
402409

403410
// Do not set a error message for a input group like radio buttons or
404411
// checkboxes where one has already been set.
405-
const inputs = this.inputs.filter((it) => it.name === input.name);
412+
const inputs = [...this.form.elements].filter((_input) => _input.name === input.name);
406413
if (inputs.length > 1 && inputs.some((it) => !!it[KEY_ERROR_EL])) {
407414
// error message for input group already set.
408415
return;
@@ -426,7 +433,7 @@ class Pattern extends BasePattern {
426433
input[KEY_ERROR_EL] = error_node;
427434

428435
let did_disable = false;
429-
for (const it of this.disabled_elements) {
436+
for (const it of this.disableable) {
430437
// Disable for melements if they are not already disabled and which
431438
// do not have set the `formnovalidate` attribute, e.g.
432439
// `<button formnovalidate>cancel</button>`.

0 commit comments

Comments
 (0)