Skip to content

Commit 6f08bd7

Browse files
committed
tech(pat-validation): Generalize the method to retrieve siblings.
1 parent d10e21c commit 6f08bd7

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/pat/validation/validation.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ class Pattern extends BasePattern {
132132
);
133133
}
134134

135+
siblings(input) {
136+
// Get all siblings of an input with the same name.
137+
return this.inputs.filter((_input) => _input.name === input.name);
138+
}
139+
135140
validate_all(event) {
136141
// Check all inputs.
137142
for (const input of this.inputs) {
@@ -297,17 +302,7 @@ class Pattern extends BasePattern {
297302
const max_values = input_options.maxValues !== null && parseInt(input_options.maxValues, 10) || null;
298303

299304
let number_values = 0;
300-
for (const _inp of this.form.elements) {
301-
// Filter for siblings with same name.
302-
if (
303-
// Keep only inputs with same name
304-
_inp.name !== input.name
305-
// Skip all form elements which are no input elements
306-
|| ! ["INPUT", "SELECT", "TEXTAREA"].includes(_inp.tagName)
307-
) {
308-
continue;
309-
}
310-
305+
for (const _inp of this.siblings(input)) {
311306
// Check if checkboxes or radios are checked ...
312307
if (_inp.type === "checkbox" || _inp.type === "radio") {
313308
if (_inp.checked) {
@@ -433,8 +428,7 @@ class Pattern extends BasePattern {
433428
msg = msg.replace(/%{value}/g, JSON.stringify(input.value));
434429

435430
// Set the error state the input itself and on all siblings, if any.
436-
const inputs = [...this.form.elements].filter((_input) => _input.name === input.name);
437-
for (const _input of inputs) {
431+
for (const _input of this.siblings(input)) {
438432
_input.setCustomValidity(msg);
439433
}
440434
// Store the error message on the input.
@@ -458,7 +452,7 @@ class Pattern extends BasePattern {
458452
let inputs = [input];
459453
if (all_of_group) {
460454
// Get all inputs with the same name - e.g. radio buttons, checkboxes.
461-
inputs = [...this.form.elements].filter((_input) => _input.name === input.name);
455+
inputs = this.siblings(input);
462456
}
463457
for (const it of inputs) {
464458
if (clear_state) {
@@ -495,7 +489,7 @@ class Pattern extends BasePattern {
495489

496490
// Do not set a error message for a input group like radio buttons or
497491
// checkboxes where one has already been set.
498-
const inputs = [...this.form.elements].filter((_input) => _input.name === input.name);
492+
const inputs = this.siblings(input);
499493
if (inputs.length > 1 && inputs.some((it) => !!it[KEY_ERROR_EL])) {
500494
// error message for input group already set.
501495
return;

0 commit comments

Comments
 (0)