Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/form/addon/components/document-validity.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export default class DocumentValidity extends Component {

for (const field of this.args.document.fields) {
yield field.validate.linked().perform();

if (field.question.hasFormatValidators) {
yield field.save.linked().perform();
}
}

if (this.isValid) {
Expand Down
38 changes: 26 additions & 12 deletions packages/form/addon/gql/fragments/field.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ fragment SimpleQuestion on Question {
}
placeholder
formatValidators {
edges {
node {
slug
regex
}
}
totalCount
}
hintText
}
Expand All @@ -33,12 +28,7 @@ fragment SimpleQuestion on Question {
}
placeholder
formatValidators {
edges {
node {
slug
regex
}
}
totalCount
}
hintText
}
Expand All @@ -50,6 +40,9 @@ fragment SimpleQuestion on Question {
value
}
placeholder
formatValidators {
totalCount
}
hintText
}
... on FloatQuestion {
Expand All @@ -61,6 +54,9 @@ fragment SimpleQuestion on Question {
value
}
placeholder
formatValidators {
totalCount
}
hintText
}
... on ChoiceQuestion {
Expand All @@ -79,6 +75,9 @@ fragment SimpleQuestion on Question {
id
value
}
formatValidators {
totalCount
}
hintText
}
... on MultipleChoiceQuestion {
Expand All @@ -97,19 +96,31 @@ fragment SimpleQuestion on Question {
id
value
}
formatValidators {
totalCount
}
hintText
}
... on DynamicChoiceQuestion {
formatValidators {
totalCount
}
hintText
}
... on DynamicMultipleChoiceQuestion {
formatValidators {
totalCount
}
hintText
}
... on DateQuestion {
dateDefaultAnswer: defaultAnswer {
id
value
}
formatValidators {
totalCount
}
hintText
}
... on StaticQuestion {
Expand All @@ -120,6 +131,9 @@ fragment SimpleQuestion on Question {
hintText
}
... on FilesQuestion {
formatValidators {
totalCount
}
hintText
}
... on ActionButtonQuestion {
Expand Down
4 changes: 4 additions & 0 deletions packages/form/addon/lib/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,8 @@ export default class Question extends Base {
].includes(this.raw.__typename))
);
}

get hasFormatValidators() {
return (this.raw.formatValidators?.totalCount ?? 0) > 0;
}
}
2 changes: 2 additions & 0 deletions packages/form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"ember-load-initializers": "3.0.1",
"ember-qunit": "9.0.4",
"ember-resolver": "13.1.1",
"ember-sinon-qunit": "7.5.0",
"ember-source": "6.1.0",
"ember-source-channel-url": "3.0.0",
"ember-try": "4.0.0",
Expand All @@ -75,6 +76,7 @@
"qunit": "2.24.1",
"qunit-dom": "3.5.0",
"sass": "1.93.2",
"sinon": "21.0.0",
"uikit": "3.23.13",
"uuid": "13.0.0",
"webpack": "5.101.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module(
const validField = new (class {
@tracked isValid = true;

question = { hasFormatValidators: false };

@restartableTask
*validate() {
yield assert.step("validate");
Expand All @@ -35,7 +37,7 @@ module(
return !this.isValid;
}

question = { raw: { label: "foo" } };
question = { raw: { label: "foo" }, hasFormatValidators: false };

@restartableTask
validate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { render, click, scrollTo, settled } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupMirage } from "ember-cli-mirage/test-support";
import { module, test } from "qunit";
import { stub } from "sinon";

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

Expand Down Expand Up @@ -100,4 +101,36 @@ module("Integration | Component | document-validity", function (hooks) {
await click("button");
await assert.verifySteps(["valid"]);
});

test("it can be triggered manually", async function (assert) {
stub(this.field.question, "hasFormatValidators").get(() => true);
stub(this.field.save, "linked").returnsThis();
stub(this.field.save, "perform").callsFake(() => {
this.field._errors = [
{
type: "format",
context: { errorMsg: "Error!" },
value: "test",
},
];

assert.step("save");
});

await render(hbs`<DocumentValidity @document={{this.document}} as |isValid validate|>
<p>
{{#if isValid}}
Valid
{{else}}
Invalid
{{/if}}
</p>
<button type="button" {{on "click" validate}}>Validate!</button>
</DocumentValidity>`);

assert.dom("p").hasText("Valid");
await click("button");
assert.dom("p").hasText("Invalid");
assert.verifySteps(["save"]);
});
});
3 changes: 3 additions & 0 deletions packages/form/tests/test-helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { setApplication } from "@ember/test-helpers";
import { start, setupEmberOnerrorValidation } from "ember-qunit";
import { loadTests } from "ember-qunit/test-loader";
import setupSinon from "ember-sinon-qunit";
import * as QUnit from "qunit";
import { setup } from "qunit-dom";

Expand All @@ -13,4 +14,6 @@ setupEmberOnerrorValidation();
loadTests();
setup(QUnit.assert);

setupSinon();

start();
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.