Skip to content

Commit c119f67

Browse files
committed
Fix logic, extend functionality.
1 parent 6c140b0 commit c119f67

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/pat/validation/validation.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,35 +260,49 @@ class Pattern extends BasePattern {
260260
this.check_input({ input: not_before_el, stop: true });
261261
}
262262
} else if (input_options.minValues || input_options.maxValues) {
263-
264263
const min_values = input_options.minValues !== null && parseInt(input_options.minValues, 10)
265264
const max_values = input_options.maxValues !== null && parseInt(input_options.maxValues, 10)
266265

267-
// This makes only sense for inputs with the same name.
268-
const valued_siblings = [...this.el.elements].filter((el) => {
266+
let number_values = 0;
267+
for (const _inp of this.el.elements) {
269268
// Filter for siblings with same name.
270-
if (el.name !== input.name) {
271-
return false;
269+
if (
270+
// Keep only inputs with same name
271+
_inp.name !== input.name
272+
// Skip all form elements which are no input elements
273+
|| ! ["INPUT", "SELECT", "TEXTAREA"].includes(_inp.tagName)
274+
) {
275+
continue;
272276
}
273277

274-
// Check if checkboxes or radios are selected ...
275-
if (el.type === "checkbox" || el.type === "radio") {
276-
return el.checked;
278+
// Check if checkboxes or radios are checked ...
279+
if (_inp.type === "checkbox" || _inp.type === "radio") {
280+
if (_inp.checked) {
281+
number_values++;
282+
}
283+
continue;
277284
}
278285

279-
// ... or inputs have a value set.
280-
// TODO: are multiple non-check/radio inputs with same name even possible?
281-
return el.value !== undefined;
282-
});
286+
// Select, if select is selected.
287+
if (_inp.tagName === "SELECT") {
288+
number_values += _inp.selectedOptions.length;
289+
continue;
290+
}
291+
292+
// For the rest a value must be set.
293+
if (_inp.value === 0 || _inp.value) {
294+
number_values++;
295+
}
296+
}
283297

284-
if (max_values !== null && valued_siblings.length > max_values) {
298+
if (max_values !== null && number_values > max_values) {
285299
this.set_error({
286300
input: input,
287301
msg: input_options.message["max-values"],
288302
max: max_values,
289303
})
290304
}
291-
if (min_values !== null && valued_siblings.length < min_values) {
305+
if (min_values !== null && number_values < min_values) {
292306
this.set_error({
293307
input: input,
294308
msg: input_options.message["min-values"],

0 commit comments

Comments
 (0)