Skip to content

Commit 27a28e5

Browse files
committed
fix(form): trigger format validators on question validation
1 parent a60c85f commit 27a28e5

File tree

8 files changed

+81
-13
lines changed

8 files changed

+81
-13
lines changed

packages/form/addon/components/document-validity.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export default class DocumentValidity extends Component {
5454

5555
for (const field of this.args.document.fields) {
5656
yield field.validate.linked().perform();
57+
58+
if (field.question.hasFormatValidators) {
59+
yield field.save.linked().perform();
60+
}
5761
}
5862

5963
if (this.isValid) {

packages/form/addon/gql/fragments/field.graphql

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ fragment SimpleQuestion on Question {
1515
}
1616
placeholder
1717
formatValidators {
18-
edges {
19-
node {
20-
slug
21-
regex
22-
}
23-
}
18+
totalCount
2419
}
2520
hintText
2621
}
@@ -33,12 +28,7 @@ fragment SimpleQuestion on Question {
3328
}
3429
placeholder
3530
formatValidators {
36-
edges {
37-
node {
38-
slug
39-
regex
40-
}
41-
}
31+
totalCount
4232
}
4333
hintText
4434
}
@@ -50,6 +40,9 @@ fragment SimpleQuestion on Question {
5040
value
5141
}
5242
placeholder
43+
formatValidators {
44+
totalCount
45+
}
5346
hintText
5447
}
5548
... on FloatQuestion {
@@ -61,6 +54,9 @@ fragment SimpleQuestion on Question {
6154
value
6255
}
6356
placeholder
57+
formatValidators {
58+
totalCount
59+
}
6460
hintText
6561
}
6662
... on ChoiceQuestion {
@@ -79,6 +75,9 @@ fragment SimpleQuestion on Question {
7975
id
8076
value
8177
}
78+
formatValidators {
79+
totalCount
80+
}
8281
hintText
8382
}
8483
... on MultipleChoiceQuestion {
@@ -97,19 +96,31 @@ fragment SimpleQuestion on Question {
9796
id
9897
value
9998
}
99+
formatValidators {
100+
totalCount
101+
}
100102
hintText
101103
}
102104
... on DynamicChoiceQuestion {
105+
formatValidators {
106+
totalCount
107+
}
103108
hintText
104109
}
105110
... on DynamicMultipleChoiceQuestion {
111+
formatValidators {
112+
totalCount
113+
}
106114
hintText
107115
}
108116
... on DateQuestion {
109117
dateDefaultAnswer: defaultAnswer {
110118
id
111119
value
112120
}
121+
formatValidators {
122+
totalCount
123+
}
113124
hintText
114125
}
115126
... on StaticQuestion {
@@ -120,6 +131,9 @@ fragment SimpleQuestion on Question {
120131
hintText
121132
}
122133
... on FilesQuestion {
134+
formatValidators {
135+
totalCount
136+
}
123137
hintText
124138
}
125139
... on ActionButtonQuestion {

packages/form/addon/lib/question.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,8 @@ export default class Question extends Base {
192192
].includes(this.raw.__typename))
193193
);
194194
}
195+
196+
get hasFormatValidators() {
197+
return (this.raw.formatValidators?.totalCount ?? 0) > 0;
198+
}
195199
}

packages/form/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"ember-load-initializers": "3.0.1",
6868
"ember-qunit": "9.0.4",
6969
"ember-resolver": "13.1.1",
70+
"ember-sinon-qunit": "7.5.0",
7071
"ember-source": "6.1.0",
7172
"ember-source-channel-url": "3.0.0",
7273
"ember-try": "4.0.0",
@@ -75,6 +76,7 @@
7576
"qunit": "2.24.1",
7677
"qunit-dom": "3.5.0",
7778
"sass": "1.93.2",
79+
"sinon": "21.0.0",
7880
"uikit": "3.23.13",
7981
"uuid": "13.0.0",
8082
"webpack": "5.101.3"

packages/form/tests/integration/components/cf-field/input/action-button-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module(
2121
const validField = new (class {
2222
@tracked isValid = true;
2323

24+
question = { hasFormatValidators: false };
25+
2426
@restartableTask
2527
*validate() {
2628
yield assert.step("validate");
@@ -35,7 +37,7 @@ module(
3537
return !this.isValid;
3638
}
3739

38-
question = { raw: { label: "foo" } };
40+
question = { raw: { label: "foo" }, hasFormatValidators: false };
3941

4042
@restartableTask
4143
validate() {

packages/form/tests/integration/components/document-validity-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { render, click, scrollTo, settled } from "@ember/test-helpers";
22
import { hbs } from "ember-cli-htmlbars";
33
import { setupMirage } from "ember-cli-mirage/test-support";
44
import { module, test } from "qunit";
5+
import { stub } from "sinon";
56

67
import { rawDocumentWithWorkItem } from "../../unit/lib/data";
78

@@ -100,4 +101,36 @@ module("Integration | Component | document-validity", function (hooks) {
100101
await click("button");
101102
await assert.verifySteps(["valid"]);
102103
});
104+
105+
test("it can be triggered manually", async function (assert) {
106+
stub(this.field.question, "hasFormatValidators").get(() => true);
107+
stub(this.field.save, "linked").returnsThis();
108+
stub(this.field.save, "perform").callsFake(() => {
109+
this.field._errors = [
110+
{
111+
type: "format",
112+
context: { errorMsg: "Error!" },
113+
value: "test",
114+
},
115+
];
116+
117+
assert.step("save");
118+
});
119+
120+
await render(hbs`<DocumentValidity @document={{this.document}} as |isValid validate|>
121+
<p>
122+
{{#if isValid}}
123+
Valid
124+
{{else}}
125+
Invalid
126+
{{/if}}
127+
</p>
128+
<button type="button" {{on "click" validate}}>Validate!</button>
129+
</DocumentValidity>`);
130+
131+
assert.dom("p").hasText("Valid");
132+
await click("button");
133+
assert.dom("p").hasText("Invalid");
134+
assert.verifySteps(["save"]);
135+
});
103136
});

packages/form/tests/test-helper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { setApplication } from "@ember/test-helpers";
22
import { start, setupEmberOnerrorValidation } from "ember-qunit";
33
import { loadTests } from "ember-qunit/test-loader";
4+
import setupSinon from "ember-sinon-qunit";
45
import * as QUnit from "qunit";
56
import { setup } from "qunit-dom";
67

@@ -13,4 +14,6 @@ setupEmberOnerrorValidation();
1314
loadTests();
1415
setup(QUnit.assert);
1516

17+
setupSinon();
18+
1619
start();

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)