Skip to content

Commit

Permalink
Merge pull request #5272 from pavinduLakshan/fix_autofill_prevent_sub…
Browse files Browse the repository at this point in the history
…mit_issue
  • Loading branch information
pavinduLakshan authored Jan 21, 2024
2 parents 73ced25 + 99a993a commit 9401438
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
8 changes: 8 additions & 0 deletions .changeset/fast-stingrays-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@wso2is/forms": patch
"@wso2is/console": patch
"@wso2is/myaccount": patch
"@wso2is/common": patch
---

Fix form autofill preventing form submission
63 changes: 27 additions & 36 deletions modules/forms/src/forms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ export const Forms: React.FunctionComponent<React.PropsWithChildren<FormPropsInt

const initialValues = useRef(new Map<string, FormValue>());

// This specifies if a form field is currently validating or not.
const [ isValidating, setIsValidating ] = useState(false);
const isValidatingRef = useRef(false);

// This holds all the form field components
Expand Down Expand Up @@ -279,14 +277,12 @@ export const Forms: React.FunctionComponent<React.PropsWithChildren<FormPropsInt
isValidatingRef.current = true;
tempModifyingFields.set(name, false);
tempTouchedFields.set(name, true);
setIsValidating(true);
setTouchedFields(tempTouchedFields);
await validate(name, tempRequiredFields, tempValidFields);

validFieldsRef.current = new Map(tempValidFields);
requiredFieldsRef.current = new Map(tempRequiredFields);
isValidatingRef.current = false;
setIsValidating(false);

setValidFields(tempValidFields);
setRequiredFields(tempRequiredFields);
Expand Down Expand Up @@ -467,39 +463,38 @@ export const Forms: React.FunctionComponent<React.PropsWithChildren<FormPropsInt
locked = false;
};

/**
* Checks if all the required fields are filled
*/
const checkRequiredFieldsFilled = (): boolean => {
let requiredFilled = true;
useEffect(() => {
if (startSubmission) {
const requiredFields = new Map(requiredFieldsRef.current);
const validFields = new Map(validFieldsRef.current);

requiredFieldsRef.current.forEach((requiredFieldParam) => {
if (!requiredFieldParam) {
requiredFilled = false;
}
});
isValidatingRef.current = true;

return requiredFilled;
};
form.forEach(async (_value, name) => {
await validate(name, requiredFields, validFields);
});

/**
* Checks if all the fields are validated
*/
const checkValidated = (): boolean => {
let isValidated = true;
isValidatingRef.current = false;

validFieldsRef.current.forEach((validField) => {
if (!validField.isValid) {
isValidated = false;
}
});
// Check whether all required fields are filled.
let areAllRequiredFieldsFilled = true;

return isValidated;
};
requiredFields.forEach((requiredFieldParam) => {
if (!requiredFieldParam) {
areAllRequiredFieldsFilled = false;
}
});

useEffect(() => {
if (startSubmission && !isValidatingRef.current) {
if (checkRequiredFieldsFilled() && checkValidated()) {
// Check whether all fields contain valid inputs.
let areAllFieldsValid = true;

validFields.forEach((validField) => {
if (!validField.isValid) {
areAllFieldsValid = false;
}
});

if (areAllRequiredFieldsFilled && areAllFieldsValid) {
setStartSubmission(false);
setIsSubmitting(false);
onSubmit && onSubmit(form);
Expand All @@ -508,12 +503,8 @@ export const Forms: React.FunctionComponent<React.PropsWithChildren<FormPropsInt
setIsSubmitting(true);
setStartSubmission(false);
}
} else {
if (startSubmission) {
setIsSubmitting(true);
}
}
}, [ startSubmission, isValidating ]);
}, [ startSubmission ]);

/**
* This validates the form and calls the `onSubmit` prop function
Expand Down

0 comments on commit 9401438

Please sign in to comment.