Skip to content

Commit 8838da0

Browse files
committed
feat(pat-validation): Validate also newly added form elements.
When form elements were added via user interaction - e.g. by using pat-clone or pat-inject - those elements were not validated. Now the form validation is re-initialized after a `pat-update` event and this problem is fixed.
1 parent a8b7981 commit 8838da0

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/pat/validation/validation.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Patterns validate - Form vlidation
22
import "../../core/polyfills"; // SubmitEvent.submitter for Safari < 15.4 and jsDOM
3+
import $ from "jquery";
34
import Base from "../../core/base";
45
import Parser from "../../core/parser";
56
import dom from "../../core/dom";
@@ -44,17 +45,39 @@ export default Base.extend({
4445

4546
init() {
4647
this.options = parser.parse(this.el, this.options);
48+
events.add_event_listener(
49+
this.el,
50+
"submit",
51+
`pat-validation--submit--validator`,
52+
(e) => {
53+
// On submit, check all.
54+
// Immediate, non-debounced check with submit. Otherwise submit
55+
// is not cancelable.
56+
for (const input of this.inputs) {
57+
logger.debug("Checking input for submit", input, e);
58+
this.check_input({ input: input, event: e });
59+
}
60+
}
61+
);
62+
63+
this.initialize_inputs();
64+
$(this.el).on("pat-update", () => {
65+
this.initialize_inputs();
66+
});
67+
68+
// Set ``novalidate`` attribute to disable the browser's validation
69+
// bubbles but not disable the validation API.
70+
this.el.setAttribute("novalidate", "");
71+
},
72+
73+
initialize_inputs() {
4774
this.inputs = [
4875
...this.el.querySelectorAll("input[name], select[name], textarea[name]"),
4976
];
5077
this.disabled_elements = [
5178
...this.el.querySelectorAll(this.options.disableSelector),
5279
];
5380

54-
// Set ``novalidate`` attribute to disable the browser's validation
55-
// bubbles but not disable the validation API.
56-
this.el.setAttribute("novalidate", "");
57-
5881
for (const [cnt, input] of this.inputs.entries()) {
5982
// Cancelable debouncer.
6083
const debouncer = utils.debounce((e) => {
@@ -81,21 +104,6 @@ export default Base.extend({
81104
(e) => debouncer(e)
82105
);
83106
}
84-
85-
events.add_event_listener(
86-
this.el,
87-
"submit",
88-
`pat-validation--submit--validator`,
89-
(e) => {
90-
// On submit, check all.
91-
// Immediate, non-debounced check with submit. Otherwise submit
92-
// is not cancelable.
93-
for (const input of this.inputs) {
94-
logger.debug("Checking input for submit", input, e);
95-
this.check_input({ input: input, event: e });
96-
}
97-
}
98-
);
99107
},
100108

101109
check_input({ input, event, stop = false }) {

0 commit comments

Comments
 (0)