diff --git a/packages/core/src/components/Form.tsx b/packages/core/src/components/Form.tsx index d32c73b23a..2eb225eb3f 100644 --- a/packages/core/src/components/Form.tsx +++ b/packages/core/src/components/Form.tsx @@ -173,8 +173,8 @@ export interface FormProps; - /** If set to true, causes the `extraErrors` to become blocking when the form is submitted */ - extraErrorsBlockSubmit?: boolean; + /** If set to true, treats `extraErrors` as warnings instead of blocking form submission */ + extraErrorsAreWarnings?: boolean; /** If set to true, turns off HTML5 validation on the form; Set to `false` by default */ noHtml5Validate?: boolean; /** If set to true, turns off all validation. Set to `false` by default @@ -1216,14 +1216,14 @@ export default class Form< * @returns - True if the form is valid, false otherwise. */ validateFormWithFormData = (formData?: T): boolean => { - const { extraErrors, extraErrorsBlockSubmit, focusOnFirstError, onError } = this.props; + const { extraErrors, focusOnFirstError, onError, extraErrorsAreWarnings } = this.props; const { errors: prevErrors } = this.state; const schemaValidation = this.validate(formData); let errors = schemaValidation.errors; let errorSchema = schemaValidation.errorSchema; const schemaValidationErrors = errors; const schemaValidationErrorSchema = errorSchema; - const hasError = errors.length > 0 || (extraErrors && extraErrorsBlockSubmit); + const hasError = errors.length > 0 || (extraErrors && !extraErrorsAreWarnings); if (hasError) { if (extraErrors) { const merged = validationDataMerge(schemaValidation, extraErrors); diff --git a/packages/core/test/Form.test.tsx b/packages/core/test/Form.test.tsx index 6cc3b2d0e5..8e373ce400 100644 --- a/packages/core/test/Form.test.tsx +++ b/packages/core/test/Form.test.tsx @@ -2567,7 +2567,7 @@ describeRepeated('Form common', (createFormComponent) => { onError, focusOnFirstError, extraErrors, - extraErrorsBlockSubmit: true, + extraErrorsAreWarnings: false, }); const input = node.querySelector('input[type=text]')!; @@ -4471,11 +4471,31 @@ describe('Async errors', () => { }, } as unknown as ErrorSchema; - const { node, onSubmit } = createFormComponent({ schema, extraErrors }); + const { node, onSubmit } = createFormComponent({ schema, extraErrors, extraErrorsAreWarnings: true }); fireEvent.submit(node); expect(onSubmit).toHaveBeenCalledTimes(1); }); + it('should block submit by default when extraErrors are present', () => { + const onError = jest.fn(); + const onSubmit = jest.fn(); + const extraErrors = { + __errors: ['blocking error'], + } as ErrorSchema; + + const { node } = createFormComponent({ + schema: {}, + onError, + onSubmit, + extraErrors, + // No extraErrorsAreWarnings prop, so should block by default + }); + + fireEvent.submit(node); + expect(onSubmit).not.toHaveBeenCalled(); + expect(onError).toHaveBeenCalled(); + }); + it('should reset when props extraErrors changes and noValidate is true', () => { const schema: RJSFSchema = { type: 'object',