Rendering forms
Last updated: · Published:
In Plate templates you can use the form Liquid tag. This tag renders an HTML form that sends an email to the specified address and saves the message to the database—so it’ll appear under Messages in the site dashboard. Inside the form tag, the submitted_form_fields object is available.
Arguments
The form tag accepts the following arguments:
for
The Plate object representing the form. (Required)to
The email address(es) where the message is sent. (Required)
Acceptable formats:- A string with one or more comma-separated email addresses.
- An array of strings, each an individual email address.
subject
Optional. The subject line for the email sent.
Default: “Message sent from%{site}”
See register_form_confirmation_field for more on confirmation email subjects.error_msg
Optional. The error message used when the form validation fails (available viarequest.alert).
Default: “Something went wrong while sending the message.”success_msg
Optional. The message shown when the form is successfully submitted (available viarequest.notice).
Default: “Thank you for your message. We will reply as soon as possible.”success_url
Optional. The URL to redirect users to after successful submission.
Default: The current page reload.enable_recaptcha(trueorskip_js)
Optional. Adds invisible Google reCAPTCHA v3 to prevent spam.- Use
truefor standard implementation. - Use
skip_jsif you generate the token yourself. - Plate does not automatically include the required Google credits—you must do this manually.
Important: Add
{% include_plate_scripts "recaptcha_scripts" %}on the page, or the form will not submit if reCAPTCHA is enabled.- Use
output_as
Optional. Currently only accepts'json'.
Returns a JSON hash suitable for POST requests to theform_messagesJSON endpoint.
Example form
{% comment %}Input{% endcomment %}
{% form for: form, to: form.email, success_url: form.success_page_link, enable_recaptcha: true %}
{% for field in form.field_lines %}
{% include field %}
{% endfor %}
<div>
<input type="submit">
</div>
{% endform %}
{% comment %}Output{% endcomment %}
<form action="/form_messages" method="post" accept-charset="utf-8">
<!-- Fields -->
<div><input type="submit" value="Send"></div>
</form>
{% comment %}You could add all kinds of attributes like so:{% endcomment %}
{% form class: "contact-form", autocomplete: true %}
submitted_form_fields object
Within the form block, you have access to the submitted_form_fields Liquid object, which provides the non-file field values submitted. This is useful for preserving form input when validation fails.
Example:
{% form ... %}
{% for field in form.fields %}
<input
type="{{ field.type }}"
name="{{ field.name | form_input_name }}"
value="{{ submitted_form_fields[field.name] }}"
/>
{% register_form_field field.name %}
{% endfor %}
{% endform %}
By setting the value of the input field to the value in the submitted_form_fields object, this input field will retain its value when the form is rendered again after a validation error on submitting the form.