Form Queries
You can fetch forms in your templates or PHP code using form queries.
{# Create a new form query #}
{% set formQuery = craft.formie.forms() %}// Create a new form query
$formQuery = \verbb\formie\elements\Form::find();Once you’ve created a query, set any parameters you need, then fetch the result with .one() or .all(). A form query returns Form objects unless you use asArray().
Formie form queries build on Craft element queries. See Element Queries (opens new window) in the Craft docs if you want the broader query syntax.
Fetch a form by handle
The most common use is fetching a form by its handle, then passing it to a render function or reading its properties.
{% set form = craft.formie.forms()
.handle('contactForm')
.one() %}
{% if form %}
{{ craft.formie.renderForm(form) }}
{% endif %}$form = \verbb\formie\elements\Form::find()
->handle('contactForm')
->one();Parameters
Form queries support Formie-specific parameters as well as Craft’s standard element query parameters.
| Param | Description |
|---|---|
handle | Narrows the query by form handle. |
layoutId | Narrows the query by the form’s field layout ID. |
pageCount | Narrows the query by the number of pages in the form. |
template | Narrows the query by form template handle or template object. |
templateId | Narrows the query by form template ID. |
asArray | Returns arrays instead of Form objects. |
dateCreated | Narrows the query by creation date. |
dateUpdated | Narrows the query by last-updated date. |
fixedOrder | Returns results in the order specified by id. |
id | Narrows the query by element ID. |
inReverse | Reverses the result order. |
limit | Limits the number of results. |
offset | Skips a number of results. |
orderBy | Sets the result order. |
title | Narrows the query by form title. |
trashed | Returns soft-deleted forms. |
uid | Narrows the query by UID. |
handle
Narrows the query by form handle.
{% set form = craft.formie.forms()
.handle('contactForm')
.one() %}$form = \verbb\formie\elements\Form::find()
->handle('contactForm')
->one();layoutId
Narrows the query by the form’s field layout ID.
{% set forms = craft.formie.forms()
.layoutId(12)
.all() %}$forms = \verbb\formie\elements\Form::find()
->layoutId(12)
->all();pageCount
Narrows the query by the number of pages in the form.
{% set multiPageForms = craft.formie.forms()
.pageCount('> 1')
.all() %}$multiPageForms = \verbb\formie\elements\Form::find()
->pageCount('> 1')
->all();template
Narrows the query by form template handle or by a form template object.
{% set forms = craft.formie.forms()
.template('contact')
.all() %}$forms = \verbb\formie\elements\Form::find()
->template('contact')
->all();templateId
Narrows the query by form template ID.
{% set forms = craft.formie.forms()
.templateId(1)
.all() %}$forms = \verbb\formie\elements\Form::find()
->templateId(1)
->all();asArray
Returns arrays of data instead of Form objects.
{% set forms = craft.formie.forms()
.asArray()
.all() %}$forms = \verbb\formie\elements\Form::find()
->asArray()
->all();dateCreated
Narrows the query by the forms’ creation dates.
{% set start = date('first day of last month')|atom %}
{% set end = date('first day of this month')|atom %}
{% set forms = craft.formie.forms()
.dateCreated(['and', ">= #{start}", "< #{end}"])
.all() %}$start = new \DateTime('first day of last month')->format(\DateTime::ATOM);
$end = new \DateTime('first day of this month')->format(\DateTime::ATOM);
$forms = \verbb\formie\elements\Form::find()
->dateCreated(['and', ">= {$start}", "< {$end}"])
->all();dateUpdated
Narrows the query by the forms’ last-updated dates.
{% set lastWeek = date('1 week ago')|atom %}
{% set forms = craft.formie.forms()
.dateUpdated(">= #{lastWeek}")
.all() %}$lastWeek = new \DateTime('1 week ago')->format(\DateTime::ATOM);
$forms = \verbb\formie\elements\Form::find()
->dateUpdated(">= {$lastWeek}")
->all();fixedOrder
Returns results in the same order as the IDs passed to id.
{% set forms = craft.formie.forms()
.id([3, 1, 2])
.fixedOrder()
.all() %}$forms = \verbb\formie\elements\Form::find()
->id([3, 1, 2])
->fixedOrder()
->all();id
Narrows the query by element ID.
{% set form = craft.formie.forms()
.id(1)
.one() %}$form = \verbb\formie\elements\Form::find()
->id(1)
->one();inReverse
Reverses the result order.
{% set forms = craft.formie.forms()
.inReverse()
.all() %}$forms = \verbb\formie\elements\Form::find()
->inReverse()
->all();limit
Limits the number of forms returned.
{% set forms = craft.formie.forms()
.limit(10)
.all() %}$forms = \verbb\formie\elements\Form::find()
->limit(10)
->all();offset
Skips a number of results.
{% set forms = craft.formie.forms()
.offset(3)
.all() %}$forms = \verbb\formie\elements\Form::find()
->offset(3)
->all();orderBy
Sets the result order.
{% set forms = craft.formie.forms()
.orderBy('elements.dateCreated asc')
.all() %}$forms = \verbb\formie\elements\Form::find()
->orderBy('elements.dateCreated asc')
->all();title
Narrows the query by form title.
{% set forms = craft.formie.forms()
.title('*Contact*')
.all() %}$forms = \verbb\formie\elements\Form::find()
->title('*Contact*')
->all();trashed
Returns forms that have been soft-deleted.
{% set forms = craft.formie.forms()
.trashed()
.all() %}$forms = \verbb\formie\elements\Form::find()
->trashed()
->all();uid
Narrows the query by UID.
{% set form = craft.formie.forms()
.uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
.one() %}$form = \verbb\formie\elements\Form::find()
->uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
->one();