Skip to content

Commit a7720ba

Browse files
committed
feat(pat-depends): Support for required fields.
If a dependend field is required but not shown, remove the required attribute Restore it once the dependend field is shown.
1 parent fe6a864 commit a7720ba

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/pat/depends/depends.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import $ from "jquery";
22
import Base from "../../core/base";
3+
import dom from "../../core/dom";
34
import utils from "../../core/utils";
45
import logging from "../../core/logging";
56
import Parser from "../../core/parser";
@@ -34,6 +35,7 @@ export default Base.extend({
3435
}
3536

3637
let state = handler.evaluate();
38+
this.set_input_state(state);
3739
switch (options.action) {
3840
case "show":
3941
utils.hideOrShow($el, state, options, this.name);
@@ -77,6 +79,28 @@ export default Base.extend({
7779
}
7880
},
7981

82+
set_input_state(enabled) {
83+
// If not enabled, remove any `required` attributes on input fields.
84+
// Otherwise restore them.
85+
const inputs = dom.find_input(this.el);
86+
87+
for (const el of inputs) {
88+
if (enabled) {
89+
const required = el.dataset.required;
90+
if (typeof required !== "undefined") {
91+
el.setAttribute("required", required);
92+
delete el.dataset.required;
93+
}
94+
} else {
95+
const required = el.getAttribute("required", null);
96+
if (required !== null) {
97+
el.dataset.required = required;
98+
el.removeAttribute("required");
99+
}
100+
}
101+
}
102+
},
103+
80104
async onReset(event) {
81105
const dependents = $(event.target).data("patDepends.dependents");
82106
await utils.timeout(50);
@@ -130,6 +154,7 @@ export default Base.extend({
130154
const $depdendent = $(dependent);
131155
const state = handler.evaluate();
132156

157+
this.set_input_state(state);
133158
switch (options.action) {
134159
case "show":
135160
utils.hideOrShow($depdendent, state, options, this.name);

src/pat/depends/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ <h2>pat-depends with checkboxes, radiobuttons and multiselects</h2>
4545
data-pat-autosuggest="words: Smooth Cayenne, Kew, Natal Queen, Singapore Spanish, Paulista, Perolera;
4646
allow-new-words: false; max-selection-size: 1;"
4747
type="text"
48+
required
4849
/>
4950
</fieldset>
5051

0 commit comments

Comments
 (0)