Skip to content

Commit 78ff247

Browse files
committed
fix: use field instance validation in viewer core
Related to #1147
1 parent a217c3b commit 78ff247

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

packages/form-js-viewer/src/Form.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,19 @@ export class Form {
200200

201201
const getErrorPath = (id, indexes) => [id, ...Object.values(indexes || {})];
202202

203-
formFieldInstanceRegistry.getAllKeyed().forEach(({ id, valuePath, indexes }) => {
203+
formFieldInstanceRegistry.getAllKeyed().forEach((fieldInstance) => {
204+
const { id, valuePath, indexes } = fieldInstance;
205+
204206
const field = formFieldRegistry.get(id);
205207

206208
// (1) Skip disabled fields
207209
if (field.disabled) {
208210
return;
209211
}
210212

211-
// (2) Validate the field
213+
// (2) Validate the field instance
212214
const value = get(data, valuePath);
213-
const fieldErrors = validator.validateField(field, value);
215+
const fieldErrors = validator.validateFieldInstance(fieldInstance, value);
214216

215217
if (fieldErrors.length) {
216218
set(errors, getErrorPath(field.id, indexes), fieldErrors);
@@ -323,23 +325,22 @@ export class Form {
323325
/**
324326
* @internal
325327
*
326-
* @param { { field: any, indexes: object, value: any } } update
328+
* @param { { fieldInstance: any, value: any } } update
327329
*/
328330
_update(update) {
329-
const { field, indexes, value } = update;
331+
const { fieldInstance, value } = update;
330332

331-
const { data, errors } = this._getState();
333+
const { id, valuePath, indexes } = fieldInstance;
332334

333-
const validator = this.get('validator'),
334-
pathRegistry = this.get('pathRegistry');
335+
const { data, errors } = this._getState();
335336

336-
const fieldErrors = validator.validateField(field, value);
337+
const validator = this.get('validator');
337338

338-
const valuePath = pathRegistry.getValuePath(field, { indexes });
339+
const fieldErrors = validator.validateFieldInstance(fieldInstance, value);
339340

340341
set(data, valuePath, value);
341342

342-
set(errors, [field.id, ...Object.values(indexes || {})], fieldErrors.length ? fieldErrors : undefined);
343+
set(errors, [id, ...Object.values(indexes || {})], fieldErrors.length ? fieldErrors : undefined);
343344

344345
this._emit('field.updated', update);
345346

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { gridColumnClasses, prefixId } from './Util';
1212
const noop = () => false;
1313

1414
export function FormField(props) {
15-
const { field, indexes, onChange } = props;
15+
const { field, indexes, onChange: _onChange } = props;
1616

1717
const formFields = useService('formFields'),
1818
viewerCommands = useService('viewerCommands', false),
@@ -53,21 +53,26 @@ export function FormField(props) {
5353

5454
const hidden = useCondition((field.conditional && field.conditional.hide) || null);
5555

56+
const fieldInstance = useMemo(
57+
() => ({
58+
id: field.id,
59+
expressionContextInfo: localExpressionContext,
60+
valuePath,
61+
indexes,
62+
}),
63+
[field.id, valuePath, localExpressionContext, indexes],
64+
);
65+
5666
// register form field instance
5767
useEffect(() => {
5868
if (formFieldInstanceRegistry && !hidden) {
59-
const instanceId = formFieldInstanceRegistry.add({
60-
id: field.id,
61-
expressionContextInfo: localExpressionContext,
62-
valuePath,
63-
indexes,
64-
});
69+
const instanceId = formFieldInstanceRegistry.add(fieldInstance);
6570

6671
return () => {
6772
formFieldInstanceRegistry.remove(instanceId);
6873
};
6974
}
70-
}, [formFieldInstanceRegistry, field.id, localExpressionContext, valuePath, indexes, hidden]);
75+
}, [fieldInstance, formFieldInstanceRegistry, hidden]);
7176

7277
// ensures the initial validation behavior can be re-triggered upon form reset
7378
useEffect(() => {
@@ -112,15 +117,16 @@ export function FormField(props) {
112117
eventBus.fire('formField.focus', { formField: field });
113118
}, [eventBus, field]);
114119

115-
const onChangeIndexed = useCallback(
120+
const onChange = useCallback(
116121
(update) => {
117-
// any data change will trigger validation
118-
setInitialValidationTrigger(false);
122+
if (!fieldConfig.keyed) {
123+
return;
124+
}
119125

120-
// add indexes of the keyed field to the update, if any
121-
onChange(fieldConfig.keyed ? { ...update, indexes } : update);
126+
setInitialValidationTrigger(false);
127+
_onChange({ ...update, field, indexes, fieldInstance });
122128
},
123-
[onChange, fieldConfig.keyed, indexes],
129+
[_onChange, field, fieldConfig.keyed, fieldInstance, indexes],
124130
);
125131

126132
if (hidden) {
@@ -136,7 +142,7 @@ export function FormField(props) {
136142
disabled={disabled}
137143
errors={fieldErrors}
138144
domId={domId}
139-
onChange={disabled || readonly ? noop : onChangeIndexed}
145+
onChange={disabled || readonly ? noop : onChange}
140146
onBlur={disabled || readonly ? noop : onBlur}
141147
onFocus={disabled || readonly ? noop : onFocus}
142148
readonly={readonly}

0 commit comments

Comments
 (0)