Skip to content

Commit

Permalink
feat: add validateOnMount to FormBase
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Jun 9, 2022
1 parent 4980a60 commit 6235eaa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion app/components/Form/FormBase.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import formTheme from "lib/theme/FormWithTheme";
import { forwardRef, useMemo } from "react";
import { forwardRef, useEffect, useMemo, useState } from "react";
import { FormProps, AjvError, withTheme } from "@rjsf/core";
import validateFormData from "@rjsf/core/dist/cjs/validate";
import { customTransformErrors } from "lib/theme/customTransformErrors";
import {
customFormats,
Expand All @@ -9,6 +10,7 @@ import {

interface FormPropsWithTheme<T> extends FormProps<T> {
theme?: any;
validateOnMount?: boolean;
}

const FormBase: React.ForwardRefRenderFunction<any, FormPropsWithTheme<any>> = (
Expand All @@ -19,15 +21,32 @@ const FormBase: React.ForwardRefRenderFunction<any, FormPropsWithTheme<any>> = (
() => withTheme(props.theme ?? formTheme),
[props.theme]
);

const transformErrors = (errors: AjvError[]) => {
return customTransformErrors(errors, customFormatsErrorMessages);
};

const [extraErrors, setExtraErrors] = useState<any>();
useEffect(() => {
if (props.validateOnMount) {
setExtraErrors(
validateFormData(
props.formData,
props.schema,
props.validate,
transformErrors
).errorSchema
);
}
}, []);

return (
<>
<Form
{...props}
// @ts-ignore
ref={ref}
extraErrors={extraErrors}
customFormats={customFormats}
transformErrors={transformErrors}
noHtml5Validate
Expand Down
2 changes: 2 additions & 0 deletions app/components/Form/ProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const ProjectForm: React.FC<Props> = (props) => {
id
rowId
newFormData
changeStatus
isUniqueValue(columnName: "proposalReference")
formChangeByPreviousFormChangeId {
changeStatus
Expand Down Expand Up @@ -235,6 +236,7 @@ const ProjectForm: React.FC<Props> = (props) => {
{...props}
schema={schema}
uiSchema={uiSchema}
validateOnMount={revision.projectFormChange.changeStatus === "staged"}
validate={uniqueProposalReferenceValidation}
formData={revision.projectFormChange.newFormData}
formContext={{
Expand Down
1 change: 1 addition & 0 deletions app/mutations/FormChange/updateProjectFormChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const mutation = graphql`
updateFormChange(input: $input) {
formChange {
id
changeStatus
newFormData
isUniqueValue(columnName: "proposalReference")
projectRevisionByProjectRevisionId {
Expand Down

0 comments on commit 6235eaa

Please sign in to comment.