-
Notifications
You must be signed in to change notification settings - Fork 14
fix: Page visibility and server validation #5779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
🧪 Review environmenthttps://yfdmyqjglmjgpfln4mfhnats6m0buhcz.lambda-url.ca-central-1.on.aws/ |
…nc/platform-forms-client into fix/page_visibility_for_validation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds robust server-side validation and refactors the client-side validation and visibility logic to use the full form template and submitted values rather than client‐provided history.
- Refactors
validateOnSubmit
to iterate all form elements, appliescheckVisibilityRecursive
, and exposes it for server actions. - Fixes dynamic row and repeating set validation by always validating declared subelements, preventing bypass via missing values.
- Replaces
groupHistory
–based page visibility withgetVisibleGroupsBasedOnValuesRecursive
, introducesgetValuesWithMatchedIds
andfindGroupByElementId
, and updates Review/NextButton components.
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
lib/validation/validation.tsx | Refactored dynamicRow loop and validateOnSubmit iteration, replaced groupHistory checks with checkVisibilityRecursive . |
lib/tests/validation/validation.test.js | Updated fileInput test cases to match new key structure. |
lib/formContext.ts | Added getValuesWithMatchedIds , getVisibleGroupsBasedOnValuesRecursive , findGroupByElementId ; refactored checkPageVisibility . |
lib/tests/formContextRecursiveVisibility.test.ts | Enabled and trimmed legacy groupHistory tests. |
lib/tests/formContextCheckPageVisibility.test.ts | Overhauled visibility tests to cover new recursive logic. |
components/clientComponents/forms/Review/Review.tsx | Switched to getValuesWithMatchedIds + recursive visibility for review items. |
components/clientComponents/forms/NextButton/NextButton.tsx | Introduced dead-end rendering when no next action remains. |
app/(gcforms)/…/actions.ts | Called validateOnSubmit in server action for submissions. |
Comments suppressed due to low confidence (5)
lib/validation/validation.tsx:342
- [nitpick] The variable
item
actually holds the field value. Consider renaming it to something likefieldValue
and separately naming the identifier (e.g.fieldId
) for clarity.
const item = values[formElement.id];
lib/formContext.ts:150
- [nitpick] The description says "values array" but this function returns an object (
FormValues
). Please update the doc comment to accurately describe the return type.
* Returns values array but with matched ids for checkboxes and radios.
components/clientComponents/forms/NextButton/NextButton.tsx:55
- The function
hasNextAction
is not imported or defined in this file, which will cause a runtime ReferenceError. Please import or definehasNextAction
before using it.
if (formHasGroups(formRecord.form) && currentGroup && !hasNextAction(currentGroup)) {
lib/validation/validation.tsx:344
- The variables
currentGroup
andgroups
are no longer used after refactoring out the group-based skip logic. Removing these dead variables will clean up the code.
const currentGroup = values.currentGroup as string;
lib/validation/validation.tsx:187
- [nitpick] Using the array index as the error key in
rowErrors
can misalign errors if subelements are reordered. Consider usingsubElement.id.toString()
as the key to map errors consistently to each subelement.
(formElement.properties.subElements || []).forEach((subElement, index) => {
Summary | Résumé
Re: #5750
This PR adds server side validation for form submissions. It uses the same
validateOnSubmit
function that fires for client side validation, but acts on the entire values object on the server on submit.validateOnSubmit refactor
This PR also refactors the validateOnSubmit function which previously would only validate data for inputs by looping over the submitted values and validating each one against the FormElements. The problem is, you could bypass validation completely by passing an empty values object or an object not containing any valid form element IDs. Now we loop over the FormElements and validate any provided values.
repeatingSet validation
Also included is a fix for a pre-existing bug related to required inputs in a repeating set. Repeating sets had the same problem described above- since we were looping over only provided values and checking the related formElement to determine validation, simply not providing an element in values bypasses its validation. This code has been modified similarly to above, where we now loop over the SubElements and validate any provided (or not) values.
checkPageVisibility
Additionally, checkPageVisibility has been refactored to use submitted values and the form template to determine whether an input should be validated, rather than the client-provided groupHistory.
To accomplish this, a new recursive function
getVisibleGroupsBasedOnValuesRecursive
is introduced that:visibleGroups
to replacegroupHistory
Other new supporting functions:
getValuesWithMatchedIds
- returns a values object with matchedIds for radio/checkbox answers for easier nextAction parsingfindGroupByElementId
- returns the group that contains a given elementReview page
Review page has been updated to use the new getVisibleGroupsBasedOnValues for rendering the page instead of groupHistory.
@todo (future refactorings/cleanup):
getVisibleGroupsBasedOnValuesRecursive
is now being used for the Review page, can do a cleanup of all the oldgroupHistory
codegetValuesWithMatchedIds
can probably replacemapIdsToValues
but will require further refactoring