Skip to content

Commit 7213535

Browse files
committed
DRAFT: feat(pat-validation): Support definition of minimum or maximum number of selections.
1 parent 3e2ef51 commit 7213535

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/pat/validation/validation.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ parser.addArgument("message-equality", "is not equal to %{attribute}.");
2525
parser.addArgument("not-after", null);
2626
parser.addArgument("not-before", null);
2727
parser.addArgument("equality", null);
28+
parser.addArgument("min-selection", null);
29+
parser.addArgument("max-selection", null);
2830
parser.addArgument("delay", 100); // Delay before validation is done to avoid validating while typing.
2931

3032
// BBB
@@ -255,6 +257,35 @@ class Pattern extends BasePattern {
255257
logger.debug("Check `no-before` input.", not_after_el);
256258
this.check_input({ input: not_before_el, stop: true });
257259
}
260+
} else if (input_options.minSelection || input_options.maxSelection)
261+
// This makes only sense for inputs with the same name.
262+
const valued_siblings = [...this.el.elements].filter((el) => {
263+
// Filter for siblings with same name.
264+
if (el.name !== input.name) {
265+
return false;
266+
}
267+
268+
// Check if they are selected or have an value
269+
if (el.type === "checkbox" || el.type === "radio") {
270+
return el.checked;
271+
}
272+
273+
// ... or have a value set.
274+
return el.value !== undefined;
275+
});
276+
277+
if (valued_siblings > input_options.maxSelection) {
278+
this.set_error({
279+
input: input,
280+
msg: "You have exceeded the maximum number of selections."
281+
})
282+
}
283+
if (valued_siblings < input_options.minSelection) {
284+
this.set_error({
285+
input: input,
286+
msg: "You need to select at least..."
287+
})
288+
}
258289
}
259290

260291
if (!validity_state.customError) {

0 commit comments

Comments
 (0)