Skip to main content

Passing Dynamic Data to Forms

A common request is how to get dynamic template-level data passed to a Freeform form. Freeform is very capable of handling this thanks to the overrideValues parameter available to freeform.form(), form.render() or form.renderTag(), depending on how you're loading your Freeform form.

Overview

The overrideValues parameter allows you to override the value of a Freeform field at the template level, like so:

{{ craft.freeform.form("myForm", {
labelClass: "form-label",
inputClass: "form-control",
overrideValues: {
firstName: currentUser.name,
myHiddenFieldHandle: entry.id,
anotherField: "something else",
}
}).render() }}

The parameter allows you to override the value inside Text fields, or even pre-select a default option for multi-option field types (specify option values in this case). E.g.:

  • hiddenFieldHandle: entry.id - pull in an entry ID from a Craft Entry.
  • stateSelect: "AZ" - pre-select Arizona state in a State select field.
  • availability: ["tue", "thu"] - pre-check Tuesday and Thursday checkbox options in a checkbox group field type.
  • firstName: currentUser.name - pull in the currently logged in user's name into the Name field.
  • myCheckbox: true - pre-check a checkbox.

Depending on what you're wanting to do with the data, you might have parts of your form pre-selected or pre-filled out for the user based on where the form is loaded (part of a Craft entry, etc) or which user is logged in (preloading their user data). Or, it might be something you want to silently collect in a hidden field, like the current URL they're at when submitting the form, etc.

You can also dynamically send email notifications with the dynamicNotification parameter:

dynamicNotification: {
recipients: ["admin@example.com", "support@example.com"],
template: "test.html"
}

Here's a quick cheatsheet for collecting common bits of information dynamically (though you'll need to make sure the context is correct, e.g. a Craft entry is set in the template in order to collect entry data):

Logged in User Data

Include data from the logged in user in Freeform form fields:

overrideValues: {
firstName: currentUser.firstName,
lastName: currentUser.lastName,
email: currentUser.email,
}

Current URL

Include the current URL in a hidden Freeform field:

overrideValues: {
currentUrl: url(craft.app.request.pathInfo),
}

Entry Data

Include data from a given Craft Entry in Freeform form fields:

overrideValues: {
entryId: entry.id,
stateSelect: entry.state,
},
dynamicNotification: {
recipients: entry.contactEmail,
template: "test.html",
}

URL Tracking Parameters

Store UTM URL tracking parameters in your form submissions:

overrideValues: {
utm_campaign: craft.app.request.getQueryParam('utm_campaign'),
utm_source: craft.app.request.getQueryParam('utm_source'),
utm_medium: craft.app.request.getQueryParam('utm_medium')
}

Where utm_campaign, utm_source, and utm_medium are the handle names of Hidden fields added to your form layout, and assuming your URL looks something like: https://mysite.net/contact?utm_campaign=my_campaign&utm_source=google_jobs_apply&utm_medium=organic

Manually Pre-define Options

Manually override field options in your Freeform form:

overrideValues: {
availability: ["tue", "thu"],
stateSelect: "AZ",
myCheckbox: true,
},
dynamicNotification: {
recipients: ["admin@example.com", "support@example.com"],
template: "test.html",
}

Passing Freeform data to another Template Function

If you need to pass a Freeform field value (such as an ID) into another Craft function like craft.entries or craft.users in email notification templates or elsewhere, be sure to specify the Freeform field as myFreeformFieldHandle.value. So for example:

{{ craft.users.id(myFFfieldHandle.value).one().name }}

OR

{% set item = craft.entries.section(section').id(myFFfieldHandle.value).one %}