Skip to content

Commit ded5b19

Browse files
committed
fixed regression
1 parent a347257 commit ded5b19

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

packages/core/src/components/Form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ export default class Form<
441441
if (mustValidate) {
442442
const schemaValidation = this.validate(formData, schema, schemaUtils, _retrievedSchema);
443443
errors = schemaValidation.errors;
444-
// If the schema has changed, we do not merge state.errorSchema.
444+
// If retrievedSchema is undefined which means the schema or formData has changed, we do not merge state.
445445
// Else in the case where it hasn't changed, we merge 'state.errorSchema' with 'schemaValidation.errorSchema.' This done to display the raised field error.
446-
if (isSchemaChanged) {
446+
if (retrievedSchema === undefined) {
447447
errorSchema = schemaValidation.errorSchema;
448448
} else {
449449
errorSchema = mergeObjects(

packages/core/test/ArrayField.test.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,6 +3219,27 @@ describe('ArrayField', () => {
32193219
});
32203220
});
32213221

3222+
it('Check that when formData changes, the form should re-validate', () => {
3223+
const { node, rerender, compCB } = createFormComponent({
3224+
schema,
3225+
formData: [
3226+
{
3227+
text: null,
3228+
},
3229+
],
3230+
liveValidate: true,
3231+
});
3232+
3233+
const errorMessages = node.querySelectorAll('#root_0_text__error');
3234+
expect(errorMessages).to.have.length(1);
3235+
const errorMessageContent = node.querySelector('#root_0_text__error .text-danger').textContent;
3236+
expect(errorMessageContent).to.contain('must be string');
3237+
3238+
rerender(compCB({ schema, formData: [{ text: 'test' }], liveValidate: true }));
3239+
3240+
expect(node.querySelectorAll('#root_0_text__error')).to.have.length(0);
3241+
});
3242+
32223243
it('raise an error and check if the error is displayed', () => {
32233244
const { node } = createFormComponent({
32243245
schema,

packages/core/test/ObjectField.test.jsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,25 @@ describe('ObjectField', () => {
227227
});
228228
});
229229

230+
it('Check that when formData changes, the form should re-validate', () => {
231+
const { node, rerender, compCB } = createFormComponent({
232+
schema,
233+
formData: {
234+
foo: null,
235+
},
236+
liveValidate: true,
237+
});
238+
239+
const errorMessages = node.querySelectorAll('#root_foo__error');
240+
expect(errorMessages).to.have.length(1);
241+
const errorMessageContent = node.querySelector('#root_foo__error .text-danger').textContent;
242+
expect(errorMessageContent).to.contain('must be string');
243+
244+
rerender(compCB({ schema, formData: { foo: 'test' }, liveValidate: true }));
245+
246+
expect(node.querySelectorAll('#root_foo__error')).to.have.length(0);
247+
});
248+
230249
it('raise an error and check if the error is displayed', () => {
231250
const { node } = createFormComponent({
232251
schema,

packages/core/test/StringField.test.jsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,23 @@ describe('StringField', () => {
299299
expect(node.querySelector('input').getAttribute('autocomplete')).eql('family-name');
300300
});
301301

302+
it('Check that when formData changes, the form should re-validate', () => {
303+
const { node, rerender, compCB } = createFormComponent({
304+
schema: { type: 'string' },
305+
formData: null,
306+
liveValidate: true,
307+
});
308+
309+
const errorMessages = node.querySelectorAll('#root__error');
310+
expect(errorMessages).to.have.length(1);
311+
const errorMessageContent = node.querySelector('#root__error .text-danger').textContent;
312+
expect(errorMessageContent).to.contain('must be string');
313+
314+
rerender(compCB({ schema: { type: 'string' }, formData: 'hello', liveValidate: true }));
315+
316+
expect(node.querySelectorAll('#root__error')).to.have.length(0);
317+
});
318+
302319
it('raise an error and check if the error is displayed', () => {
303320
const { node } = createFormComponent({
304321
schema: { type: 'string' },

packages/core/test/test_utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ export function createComponent(Component, props) {
1414
const onError = sinon.spy();
1515
const onSubmit = sinon.spy();
1616
const comp = <Component onSubmit={onSubmit} onError={onError} onChange={onChange} {...props} />;
17-
const { container } = render(comp);
17+
const compCB = (props) => <Component onSubmit={onSubmit} onError={onError} onChange={onChange} {...props} />;
18+
const { container, rerender } = render(comp);
1819
const node = findDOMNode(container).firstElementChild;
1920

20-
return { comp, node, onChange, onError, onSubmit };
21+
return { comp, compCB, node, onChange, onError, onSubmit, rerender };
2122
}
2223

2324
export function createFormComponent(props) {

0 commit comments

Comments
 (0)