[6.x] Forms 2: Multi-page forms & SubmitForm action#14840
Draft
duncanmcclean wants to merge 38 commits into
Draft
[6.x] Forms 2: Multi-page forms & SubmitForm action#14840duncanmcclean wants to merge 38 commits into
SubmitForm action#14840duncanmcclean wants to merge 38 commits into
Conversation
SubmitForm actionSubmitForm action & Multi-page forms
SubmitForm action & Multi-page formsSubmitForm action
SubmitForm actionSubmitForm action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request adds support for handling multi-page form submissions and implements a re-usable
SubmitFormaction.Multipage forms
Note
While this PR adds support for handling multi-page form submissions in Statamic Core, you'll need to install the Forms Pro addon (paid) to use them.
This PR adds support for handling multi-page form submissions with the
{{ form:create }}tag without needing to update your templates.The
{{ sections }}and{{ fields }}loops are automatically scoped to the current page. The page's display name, help text (instructions) and button labels are available in the form tag:{{ form:multi_page_form }} <h2>{{ page:display }}</h2> <p>{{ page:instructions }}</p> {{ if success }} Success! {{ /if }} {{ sections }} <fieldset> <legend>{{ display }}</legend> {{ form:fields }} <div> <label>{{ display }}</label> <p>{{ instructions }}</p> {{ field }} {{ if error }} <p>{{ error }}</p> {{ /if }} </div> {{ /form:fields }} </fieldset> {{ /sections }} {{ if previous_page_url }} <a href="{{ previous_page_url }}">{{ previous_page_label }}</a> {{ /if }} <button>{{ button_label }}</button> {{ /form:multi_page_form }}Upon submitting a page, Statamic will validate its fields and create a "partial" form submission (see #14832). This partial submission will be "finalized" when the final page is submitted, triggering emails, etc.
Multi-page forms can be used in conjunction with Statamic's Alpine & Alpine Precognition JS drivers.
Forms Pro will ship with its own Alpine drivers allowing you to maintain page state on the frontend and submit page values over AJAX for a more seamless experience.
Static Caching
Fields are now pre-populated using values from the user's partial submission, to ensure values are kept when navigating between pages.
For this reason, we recommend wrapping all forms in the
{{ nocache }}tag.SubmitFormaddonThis PR also extracts the form submission logic from Statamic's
FormControllerinto a reusableSubmitFormaction class, making it easier for third-party developers to submit forms via Livewire components or custom API endpoints.Methods
formFormyou want to use.pageresumeSubmissioninstance of the partial submission you wish to resume.submit$dataand an optional array of$files. Returns aSubmissionResultobject containing the submission and the ID of the next page (in the case of a multi-page form).validate$dataand an optional array of$files. Also accepts an array of field handles to limit which fields are validated.Precognition
Validation used to live in a
FrontendFormRequest, which is where Precognition was handled.However, now that validation lives in the action, the form request has been deleted and Precognition is now handled explicitly in the controller. It validates via the action (scoped to the
Precognition-Validate-Onlyfields) and halts without storing a submission.Laravel's
HandlePrecognitiveRequestsmiddleware short-circuts the controller before its code is executed, so the form route uses a slim HandleFormPrecognitiveRequests subclass which keeps Precognition detection and headers but lets the request reach the controller.Replaces #14375
Replaces #14867
Depends on #14832
Closes statamic/ideas#1409