Forms
Peak Forms live validate and submit using Laravel Precognitionopen in new window. The forms are designed around Form Design Patternsopen in new window (with a few exceptions) and they support the AlpineJS driven conditional fields featureopen in new window.
Rendering
Peak renders forms and mail templates dynamically so you can add as many forms as you'd like, just by creating them in the CP. They also support form sections you can create in the form blueprint builder. Peak ships with a default basic contact form you can edit using the following files:
resources/forms/contact.yamlThe contact form configuration along with the defined blueprint and connectors.resources/views/page_builder/_form.antlers.htmlThe page_builder's generic forms template file.resources/views/components/_form.antlers.htmlThe reusable forms template for rendering the actual form.resources/lang/en/strings.phpStrings file containing some translations that are used for rendering.
Peak comes with a PeakEmail connector that ships within the Peak Tools package. This connector uses a custom email template that can be edited by publishing the peak-tools-views. The file used are email/form.antlers.html and email/form_text.antlers.html.
The default contact form has a consent field with its visibility set to hidden. This setting hides it from the publish form of stored submissions as well as from the email. Feel free to apply the same to other fields within your blueprint.
Form email texts can be fully customized within the Control Panel.
Error handling
According to Form Design Patterns its best to place error labels below the label and above an input field. This makes sense because error labels could be obscured when the user hasn't scrolled down yet. However in the case of Precognition, errors kick in on blur, so we can assume with most certainty that the user will see the error label. Placing them below the form field prevents jumpyness. To make sure users know when they've made mistakes, after submission they will get focus on an error summary.
Form handler
The form handler view that is included in your form partial is part of the Tools Addon. If you want to make changes to the views from this addon, you can publish them by running:
php artisan vendor:publish --tag="statamic-peak-tools-views"
Success Hook
You can pass in an argument to the form handler as a success_hook-parameter or as a slot. For example: you could use this to trigger a dataLayer event if your site relies on GTM and your marketer wants to track form events.
{{ partial:statamic-peak-tools::snippets/form_handler success_hook="console.log('Hurray!')" }}
or
{{ partial:statamic-peak-tools::snippets/form_handler }}
console.log('Hurray!')
{{ /partial:statamic-peak-tools::snippets/form_handler }}
2
3
By design, there is a default success hook in place, which resets the success variable after 10 seconds, even if you pass a custom success hook. If you want to skip that or override that behaviour, simply pass a success hook with a return statement at the end.
{{ partial:statamic-peak-tools::snippets/form_handler success_hook="console.log('Hurray!'); return" }}
This will log "Hurray!" into your browser console and skip the default success hook.
Redirect to a page after submitting
If you want to redirect to a different page instead of using the default success hook after a submission has been made, you can pass a URL via the redirect parameter to the components/_form.antlers.html template. This will make use of Statamics built in behavior for redirecting after a successful submission.
{{ partial:components/form redirect="/to-success-url" }}
Submit additional data
If you want to submit additional data besides your form fields you can add further fields to the x-data attribute on the form:create tag that will be merged with the actual Alpine data object. By default, only a _redirect field is set to support the redirect behavior.
{{ form:create :in="form:handle" js="alpine:form" attr:x-ref="form" x-data='@{ "_redirect": null, "custom_field": "value" }' }}
Hidden fields
When you want to populate a hidden field with custom data, you can add the following within your form:create tag:
<div x-init="form.hidden_field_handle = 'value', form.validate('hidden_field_handle')"></div>
This will make sure that your hidden fields gets populated with the data you want and added to the precognition request. When the value comes from Alpine and needs to be dynamic you can use x-effect instead.
Captcha
You can use Aryeh Raber's wonderful Captcha addonopen in new window together with your Peak form. In order to make this work, follow the addons instructions and update your form:create tag to x-data='@{ "_redirect": null, "captcha-response": "" }' in resources/views/components/_form.antlers.html. Lastly, make sure you run the latest versions of both the Peak Tools and the Captcha addons.