Skip to content

Commit bf6023f

Browse files
committed
fix(pat-validation): Support the combination of multiple custom validation constraints.
1 parent 735ebda commit bf6023f

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/pat/validation/validation.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,13 @@ class Pattern extends BasePattern {
178178
msg: message,
179179
attribute: input_options.equality,
180180
});
181-
} else if (input_options.not.after || input_options.not.before) {
181+
}
182+
183+
if (
184+
! validity_state.customError && // No error from previous checks.
185+
input_options.not.after ||
186+
input_options.not.before
187+
) {
182188
const msg = input_options.message.date || input_options.message.datetime;
183189
const msg_default_not_before = "The date must be after %{attribute}";
184190
const msg_default_not_after = "The date must be before %{attribute}";
@@ -280,7 +286,13 @@ class Pattern extends BasePattern {
280286
logger.debug("Check `no-before` input.", not_after_el);
281287
this.check_input({ input: not_before_el, stop: true });
282288
}
283-
} else if (input_options.minValues || input_options.maxValues) {
289+
}
290+
291+
if (
292+
! validity_state.customError && // No error from previous checks.
293+
input_options.minValues ||
294+
input_options.maxValues
295+
) {
284296
const min_values = input_options.minValues !== null && parseInt(input_options.minValues, 10) || null;
285297
const max_values = input_options.maxValues !== null && parseInt(input_options.maxValues, 10) || null;
286298

src/pat/validation/validation.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,73 @@ describe("pat-validation", function () {
620620
expect(document.querySelectorAll("em.warning").length).toBe(1);
621621
expect(document.querySelector("em.warning").matches("input[name=input2] + em.warning")).toBe(true);
622622
});
623+
624+
it("1.25 - Can combine multiple custom validation rules.", async function () {
625+
document.body.innerHTML = `
626+
<form
627+
class="pat-validation"
628+
data-pat-validation="
629+
message-required: is required;
630+
message-equality: not equal;
631+
message-min-values: too few;
632+
"
633+
>
634+
<fieldset data-pat-validation="min-values: 2">
635+
<input id="i1" name="textinput" data-pat-validation="equality: extrainput" required>
636+
<input id="i2" name="textinput">
637+
</fieldset>
638+
<input id="i3" name="extrainput">
639+
</form>
640+
`;
641+
const form = document.querySelector(".pat-validation");
642+
const input1 = document.querySelector("#i1");
643+
const input2 = document.querySelector("#i2");
644+
const input3 = document.querySelector("#i3");
645+
const button = document.querySelector("button");
646+
647+
const instance = new Pattern(form);
648+
await events.await_pattern_init(instance);
649+
650+
input1.dispatchEvent(events.change_event());
651+
await utils.timeout(1); // wait a tick for async to settle.
652+
653+
// form is invalid due to input1's required attribute
654+
expect(input1.matches(":invalid")).toBe(true);
655+
expect(document.querySelectorAll("em.warning").length).toBe(1);
656+
expect(document.querySelectorAll("em.warning")[0].textContent).toBe(
657+
"is required"
658+
);
659+
660+
input1.value = "ok";
661+
input1.dispatchEvent(events.change_event());
662+
await utils.timeout(1); // wait a tick for async to settle.
663+
664+
// form is invalid due to input1's equality constraint
665+
expect(input1.matches(":invalid")).toBe(true);
666+
expect(document.querySelectorAll("em.warning").length).toBe(1);
667+
expect(document.querySelectorAll("em.warning")[0].textContent).toBe(
668+
"not equal"
669+
);
670+
671+
input3.value = "ok";
672+
input1.dispatchEvent(events.change_event());
673+
await utils.timeout(1); // wait a tick for async to settle.
674+
675+
// form is invalid due to input1's equality constraint
676+
expect(input1.matches(":invalid")).toBe(true);
677+
expect(document.querySelectorAll("em.warning").length).toBe(1);
678+
expect(document.querySelectorAll("em.warning")[0].textContent).toBe(
679+
"too few"
680+
);
681+
682+
input2.value = "ok";
683+
form.dispatchEvent(events.submit_event());
684+
await utils.timeout(1); // wait a tick for async to settle.
685+
686+
// form is now valid.
687+
expect(input1.matches(":invalid")).toBe(false);
688+
expect(document.querySelectorAll("em.warning").length).toBe(0);
689+
});
623690
});
624691

625692
describe("2 - required inputs", function () {

0 commit comments

Comments
 (0)