Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

feat: add serverside validation to form submission #61

Conversation

IAmNatch
Copy link

@IAmNatch IAmNatch commented Aug 29, 2023

Original

What it does:

  • Validates that required fields are not undefined or empty strings.
  • Validates that all fields provided by formData exist.

How it does this:

  • Uses field level validation to get the name of the field
  • Fetch the matching form, and get the record for that field.
  • If the field does not exist, report as an invalid field name.
  • If the field is required, confirm that it is not empty or undefined.

What it does not do:

  • Validate that required fields are included in the form submission. We can't know this, since the field level validation will never be executed for a field that has not been added to the array.
  • Validate that a valid formID is provided.

Performance considerations:

  • I ran a few quick tests using 2 and 6 fields, and saw no significant impact on querying for the matching form in each fields validate function.

Update

Looking at the beforeValidate hook, it does not provide an easy way to through field level validation errors. With this in mind, I thought of a solution that keeps validation in the field level, and uses the beforeValidate hook in the spirit it was intended-- to prepare for validation.

Required Fields:
In the updated implementation I've added a beforeValidate hook which looks of the form, iterates through its fields, and adds an empty record for any field which was not included in the submission. This allows the field level validator to run on these missing fields, and evaluate if they should be treated as required down the road.

Invalid Form Names:

  • I currently have a check when fetching the form in the beforeValidate hook which will return an error if no form is found. It may be preferable to do this validation in the field level too, I'll leave this up to the reviewers discretion.

Performance:

  • This new usage, while meeting of the outlined edge cases, does require some duplicated work. We fetch the form once in the collection hook, and once more for each field. This seems to add a few ms, but nothing too crazy. Happy to test further if we know we'll proceed with this PR.

@IAmNatch IAmNatch force-pushed the feat--add-server-side-validation-to-form-submission branch from 6a3af3b to f52c632 Compare August 29, 2023 01:19
@IAmNatch IAmNatch changed the title feat: add serverside validation to form submission WIP: feat: add serverside validation to form submission Aug 29, 2023
@IAmNatch IAmNatch force-pushed the feat--add-server-side-validation-to-form-submission branch from f52c632 to 43ff7f6 Compare August 29, 2023 01:44
@IAmNatch IAmNatch changed the title WIP: feat: add serverside validation to form submission feat: add serverside validation to form submission Sep 8, 2023
@jacobsfletch
Copy link
Member

This plugin is now being maintained in the Packages Directory of the Payload Monorepo. This repo will soon be archived and all open PRs including this one will be closed. Would you be willing to re-open this same PR but within the Payload monorepo? I've included a reference to this PR in this open discussion so that it will not get lost.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants