Skip to content

Commit 10dd63d

Browse files
committed
fix(pat-validation): Put error messages always at the end of a group of inputs with the same name.
1 parent 6610f70 commit 10dd63d

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/pat/validation/documentation.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ In addition both the input element and its label will get an `warning` class.
8989
</label>
9090
```
9191

92-
Checkboxes and radio buttons are treated differently: if they are contained in a fieldset with class `checklist` error messages are added at the end of the fieldset.
92+
Checkboxes and radio buttons are treated differently: The error message is alywas set after the last element of the inputs with the same name.
9393

9494
```html
95-
<fieldset class="checklist radio">
95+
<fieldset>
9696
<label><input type="radio" name="radio" /> Strawberry</label>
9797
<label><input type="radio" name="radio" /> Banana</label>
9898
<label><input type="radio" name="radio" /> Raspberry</label>

src/pat/validation/validation.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -479,15 +479,9 @@ class Pattern extends BasePattern {
479479
this.error_template(validation_message)
480480
).firstChild;
481481

482-
let fieldset;
483-
if (input.type === "radio" || input.type === "checkbox") {
484-
fieldset = input.closest("fieldset.pat-checklist");
485-
}
486-
if (fieldset) {
487-
fieldset.append(error_node);
488-
} else {
489-
input.after(error_node);
490-
}
482+
// Put error messge after the erronous input or - in case of multiple
483+
// inputs with the same name - after the last one of the group.
484+
inputs.pop().after(error_node);
491485
input[KEY_ERROR_EL] = error_node;
492486

493487
let did_disable = false;

src/pat/validation/validation.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,27 @@ describe("pat-validation", function () {
564564
expect(input.matches(":invalid")).toBe(true);
565565
expect(button.matches(":disabled")).toBe(true);
566566
});
567+
568+
it("1.23 - Puts the warning at the end of same-name inputs.", async function () {
569+
document.body.innerHTML = `
570+
<form class="pat-validation" id="form">
571+
<input name="group" type="radio" required/>
572+
<input name="group" type="radio" required/>
573+
<input name="group" type="radio" required/>
574+
</form>
575+
`;
576+
const form = document.querySelector(".pat-validation");
577+
578+
const instance = new Pattern(form);
579+
await events.await_pattern_init(instance);
580+
581+
form.dispatchEvent(events.submit_event());
582+
await utils.timeout(1); // wait a tick for async to settle.
583+
584+
const warning = form.querySelector("em.warning");
585+
expect(warning).toBeTruthy();
586+
expect(warning.matches("input:nth-child(3) + em.warning")).toBe(true);
587+
});
567588
});
568589

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

0 commit comments

Comments
 (0)