Skip to content

Commit c4fe13c

Browse files
authored
fix: setFieldValue should reset validation (#744)
1 parent 3dae665 commit c4fe13c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/useForm.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ export class FormStore {
787787
{
788788
name,
789789
value,
790+
errors: [],
791+
warnings: [],
790792
},
791793
]);
792794
};

tests/index.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,4 +996,31 @@ describe('Form.Basic', () => {
996996
expect(formRef.current?.getFieldError('light')).toHaveLength(0);
997997
expect(formRef.current?.getFieldError('bamboo')).toHaveLength(0);
998998
});
999+
1000+
it('setFieldValue should reset errors', async () => {
1001+
const formRef = React.createRef<FormRef>();
1002+
1003+
const Demo: React.FC = () => (
1004+
<Form ref={formRef}>
1005+
<Field name="light" rules={[{ validator: () => Promise.reject('No!') }]}>
1006+
<Input />
1007+
</Field>
1008+
</Form>
1009+
);
1010+
1011+
render(<Demo />);
1012+
1013+
// Mock error first
1014+
await act(async () => {
1015+
await formRef.current?.validateFields().catch(() => {});
1016+
});
1017+
expect(formRef.current?.getFieldError('light')).toHaveLength(1);
1018+
1019+
// setFieldValue
1020+
await act(async () => {
1021+
formRef.current?.setFieldValue('light', 'Bamboo');
1022+
await Promise.resolve();
1023+
});
1024+
expect(formRef.current?.getFieldError('light')).toHaveLength(0);
1025+
});
9991026
});

0 commit comments

Comments
 (0)