Skip to content
Open
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
8 changes: 4 additions & 4 deletions packages/core/src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ export interface FormProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F e
* still submit the form when these are the only errors displayed to the user.
*/
extraErrors?: ErrorSchema<T>;
/** 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
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 22 additions & 2 deletions packages/core/test/Form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2567,7 +2567,7 @@ describeRepeated('Form common', (createFormComponent) => {
onError,
focusOnFirstError,
extraErrors,
extraErrorsBlockSubmit: true,
extraErrorsAreWarnings: false,
});

const input = node.querySelector<HTMLInputElement>('input[type=text]')!;
Expand Down Expand Up @@ -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',
Expand Down