Skip to content

Commit 57061cb

Browse files
stepbackjglasserc
authored andcommitted
Fixed issue where form validation errors stay on the page after user fixed the causes (rjsf-team#844)
* fixed issue where form validation errors stay on the page even after user resolved the issues * added unit tests for reset error state bug * fix broken unit test * updated unit tests for Form_test with more assertions
1 parent 7b6b3b5 commit 57061cb

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/components/Form.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ export default class Form extends Component {
149149
}
150150
}
151151

152-
if (this.props.onSubmit) {
153-
this.props.onSubmit({ ...this.state, status: "submitted" });
154-
}
155-
this.setState({ errors: [], errorSchema: {} });
152+
setState(this, { errors: [], errorSchema: {} }, () => {
153+
if (this.props.onSubmit) {
154+
this.props.onSubmit({ ...this.state, status: "submitted" });
155+
}
156+
});
156157
};
157158

158159
getRegistry() {

test/Form_test.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,15 @@ describe("Form", () => {
269269
});
270270

271271
describe("Custom submit buttons", () => {
272-
it("should submit the form when clicked", () => {
273-
const onSubmit = sandbox.spy();
272+
it("should submit the form when clicked", done => {
273+
let submitCount = 0;
274+
const onSubmit = () => {
275+
submitCount++;
276+
if (submitCount === 2) {
277+
done();
278+
}
279+
};
280+
274281
const comp = renderIntoDocument(
275282
<Form onSubmit={onSubmit} schema={{}}>
276283
<button type="submit">Submit</button>
@@ -281,7 +288,6 @@ describe("Form", () => {
281288
const buttons = node.querySelectorAll("button[type=submit]");
282289
buttons[0].click();
283290
buttons[1].click();
284-
sinon.assert.calledTwice(onSubmit);
285291
});
286292
});
287293

@@ -1008,6 +1014,34 @@ describe("Form", () => {
10081014
})
10091015
);
10101016
});
1017+
1018+
it("should reset errors and errorSchema state to initial state after correction and resubmission", () => {
1019+
const onError = sandbox.spy();
1020+
const { comp, node } = createFormComponent({
1021+
schema,
1022+
onError,
1023+
});
1024+
1025+
Simulate.change(node.querySelector("input[type=text]"), {
1026+
target: { value: "short" },
1027+
});
1028+
Simulate.submit(node);
1029+
1030+
expect(comp.state.errorSchema).eql({
1031+
__errors: ["should NOT be shorter than 8 characters"],
1032+
});
1033+
expect(comp.state.errors.length).eql(1);
1034+
sinon.assert.calledOnce(onError);
1035+
1036+
Simulate.change(node.querySelector("input[type=text]"), {
1037+
target: { value: "long enough" },
1038+
});
1039+
Simulate.submit(node);
1040+
1041+
expect(comp.state.errorSchema).eql({});
1042+
expect(comp.state.errors).eql([]);
1043+
sinon.assert.calledOnce(onError);
1044+
});
10111045
});
10121046

10131047
describe("root level", () => {

0 commit comments

Comments
 (0)