Skip to documentation content

Building and Handling Forms

Building and Handling Forms

How to display a form, handle validation, and process submission in Liquid templates.

Forms in Marketpath CMS are Form records. Editors build the fields in the CMS; a template decides where the form appears and, only when needed, how its markup looks.

Print the form first
The usual way to show a form is to give the page a form field and print it:

{% if entity.contact_form.is_valid %}
{{ entity.contact_form }}
{% endif %}

The CMS renders the whole form: every field, its conditions and validators, the captcha, the submit button, and the script that submits it. The markup comes from the Form record's own template override, so changing the look of every form on a site is a matter of choosing or styling that template, not editing pages. Style individual controls with each field's Classname. Reach for manual rendering only when the design's markup cannot be reproduced with CSS — a wrapper or grid the CMS does not emit.

Rendering a form yourself
Load a form by name or GUID with the form method, then build the markup from its properties:

{% form var contact = "Contact Us" %}
{% if contact.is_valid %}
{{ contact.form_start }}
{% for field in contact.fields %}
<div class="form-field">
<label for="{{ field.element_id }}">{{ field.label }}</label>
{{ field }}
</div>
{% endfor %}
{{ contact.form_end }}
{% endif %}

• form_start outputs the opening form tag with the form's unique id. form_end outputs the closing tag and the script the CMS needs for conditional fields, validation, captcha, and submission. Always use both; omitting form_end breaks all of those.
• fields is the ordered list of top-level fields. A field inside a group is not in that list; iterate the group's own fields one level deeper.
• Each field has shared properties — label, field_id (also the value for an input's name), element_id, classname, subtext, condition, object_type — and each field type adds its own, such as placeholder, required, and required_message on a text field. Switch on object_type to render each type differently.
• Printing a field ({{ field }} or {{ field.output }}) outputs the CMS's default markup for that one control. Use it where the default is good enough and build the control from its properties where the design needs control over the wrapper.
• form_submit_target gives the URL to submit to when you build and submit the form entirely yourself instead of using form_start and form_end.

After submission
The CMS processes every submission. A form's redirect URL (redirect_url on the form object, set on the Form record) is where the visitor goes after a successful submission, so a thank-you page needs no template logic. See the Form, Form Field, and Form Submit references for every property.