Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: programmatic validation removes previous errors if all data is now valid #3855

Merged
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/core

- Updated `MultiSchemaField` to only merge top level required field fixing duplicate field and description.
- Fixed programmatic validation (`validateForm()`) removes previous errors if all data is now valid.

## @rjsf/chakra-ui

Expand Down
15 changes: 11 additions & 4 deletions packages/core/src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -729,13 +729,14 @@ export default class Form<
*/
validateForm() {
const { extraErrors, extraErrorsBlockSubmit, focusOnFirstError, onError } = this.props;
const { formData } = this.state;
const { formData, errors: prevErrors } = this.state;
const schemaValidation = this.validate(formData);
let errors = schemaValidation.errors;
let errorSchema = schemaValidation.errorSchema;
const schemaValidationErrors = errors;
const schemaValidationErrorSchema = errorSchema;
if (errors.length > 0 || (extraErrors && extraErrorsBlockSubmit)) {
const hasError = errors.length > 0 || (extraErrors && extraErrorsBlockSubmit);
if (hasError) {
if (extraErrors) {
const merged = validationDataMerge(schemaValidation, extraErrors);
errorSchema = merged.errorSchema;
Expand Down Expand Up @@ -763,9 +764,15 @@ export default class Form<
}
}
);
return false;
} else if (prevErrors.length > 0) {
this.setState({
errors: [],
errorSchema: {},
schemaValidationErrors: [],
schemaValidationErrorSchema: {},
});
}
return true;
return !hasError;
}

/** Renders the `Form` fields inside the <form> | `tagName` or `_internalFormWrapper`, rendering any errors if
Expand Down
43 changes: 43 additions & 0 deletions packages/core/test/Form.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4182,4 +4182,47 @@ describe('Form omitExtraData and liveOmit', () => {
expect(checkbox.checked).to.eq(true);
});
});

describe('validateForm()', () => {
it('Should update state when data updated from invalid to valid', () => {
const ref = createRef();
const props = {
schema: {
type: 'object',
required: ['input'],
properties: {
input: {
type: 'string',
},
},
},
formData: {},
ref,
};
const { comp, node } = createFormComponent(props);

// trigger programmatic validation and make sure an error appears.
expect(ref.current.validateForm()).to.eql(false);
let errors = node.querySelectorAll('.error-detail');
expect(errors).to.have.lengthOf(1);
expect(errors[0].textContent).to.be.eql("must have required property 'input'");

// populate the input and simulate a re-render from the parent.
const textNode = node.querySelector('#root_input');
Simulate.change(textNode, {
target: { value: 'populated value' },
});
setProps(comp, { ...props, formData: { input: 'populated value' } });

// error should still be present.
errors = node.querySelectorAll('.error-detail');
expect(errors).to.have.lengthOf(1);
expect(errors[0].textContent).to.be.eql("must have required property 'input'");

// trigger programmatic validation again and make sure the error disappears.
expect(ref.current.validateForm()).to.eql(true);
errors = node.querySelectorAll('.error-detail');
expect(errors).to.have.lengthOf(0);
});
});
});