Skip to content

Commit d9e767c

Browse files
committed
fix: prevent multiselects from validating early
Closes #1123
1 parent 0030341 commit d9e767c

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

packages/form-js-viewer/src/render/components/FormField.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useCallback, useContext, useEffect, useMemo, useState } from 'preact/hooks';
2+
import isEqual from 'lodash/isEqual';
23

34
import { get } from 'min-dash';
45

@@ -91,7 +92,9 @@ export function FormField(props) {
9192

9293
useEffect(() => {
9394

94-
if (initialValidationTrigger && initialValue) {
95+
const hasInitialValue = initialValue && !isEqual(initialValue, []);
96+
97+
if (initialValidationTrigger && hasInitialValue) {
9598
setInitialValidationTrigger(false);
9699
viewerCommands.updateFieldValidation(field, initialValue, indexes);
97100
}

packages/form-js-viewer/test/spec/Form.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import dynamicListTableFilterInteractionSchema from './dynamic-list-table-filter
2222
import hiddenFieldsConditionalSchema from './hidden-fields-conditional.json';
2323
import hiddenFieldsExpressionSchema from './hidden-fields-expression.json';
2424
import disabledSchema from './disabled.json';
25+
import requiredSchema from './required.json';
2526
import schema from './form.json';
2627
import groupsSchema from './groups.json';
2728
import schemaNoIds from './form.json';
@@ -581,6 +582,24 @@ describe('Form', function() {
581582
});
582583

583584

585+
it('should not trigger required validation on initial load', async function() {
586+
587+
// given
588+
const data = {};
589+
590+
// when
591+
await bootstrapForm({
592+
container,
593+
data,
594+
schema: requiredSchema
595+
});
596+
597+
// then
598+
expect(form).to.exist;
599+
expect(document.body.innerHTML).not.to.contain('Field is required.');
600+
601+
});
602+
584603
const runFocusBlurTest = function(id, index, selector) {
585604

586605
it('focus and blur events should trigger for ' + id, async function() {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"$schema": "../../../form-json-schema/resources/schema.json",
3+
"components": [
4+
{
5+
"key": "creditor",
6+
"id": "Creditor_ID",
7+
"label": "Creditor",
8+
"type": "textfield",
9+
"validate": {
10+
"required": true
11+
}
12+
},
13+
{
14+
"key": "invoiceNumber",
15+
"label": "Invoice Number",
16+
"type": "textfield",
17+
"validate": {
18+
"required": true
19+
}
20+
},
21+
{
22+
"key": "amount",
23+
"label": "Amount",
24+
"type": "number",
25+
"validate": {
26+
"required": true
27+
}
28+
},
29+
{
30+
"key": "approved",
31+
"label": "Approved",
32+
"type": "checkbox",
33+
"validate": {
34+
"required": true
35+
}
36+
},
37+
{
38+
"key": "approvedBy",
39+
"label": "Approved By",
40+
"type": "textfield",
41+
"validate": {
42+
"required": true
43+
}
44+
},
45+
{
46+
"key": "approverComments",
47+
"label": "Approver comments",
48+
"type": "textarea",
49+
"validate": {
50+
"required": true
51+
}
52+
},
53+
{
54+
"key": "product",
55+
"label": "Product",
56+
"type": "radio",
57+
"validate": {
58+
"required": true
59+
}
60+
},
61+
{
62+
"key": "mailto",
63+
"label": "Email Summary To",
64+
"type": "checklist",
65+
"validate": {
66+
"required": true
67+
}
68+
},
69+
{
70+
"key": "language",
71+
"label": "Language",
72+
"type": "select",
73+
"validate": {
74+
"required": true
75+
}
76+
},
77+
{
78+
"key": "conversation",
79+
"type": "datetime",
80+
"validate": {
81+
"required": true
82+
}
83+
},
84+
{
85+
"key": "tags",
86+
"label": "Taglist",
87+
"type": "taglist",
88+
"validate": {
89+
"required": true
90+
}
91+
}
92+
],
93+
"type": "default"
94+
}

0 commit comments

Comments
 (0)