WFFM Custom Field – Hidden Token Field

One of the most useful custom WFFM fields we use is a custom hidden token field. I’ve used this on several projects for various reasons.

I’ve seen a number of implementations which will use a regular Single Line Text field (or similar) and then hide the field either using JavaScript(?!?!) or CSS by selecting a custom style in the Form Designer. Whilst both kind of work, they’re not ideal – I’ve seen forms fields which are momentarily visible and “flicker” whilst the page loads and it looks a little weird. It also relies on the authors remembering to set the class. Besides, there’s already a hidden input type available in HTML, why not use that?

Due to the nature of how WFFM works on a multi-server environment and the way that Save Actions are processed, you won’t have access to the request context. Although it is possible to have Save Actions process on the CD server, by default they will run on the CM server via the Event Queue (or a Web Service in earlier versions). Therefore if you need additional details to be included in the submission then one option is to simply include it in the form as hidden fields which your save actions can then use.

There are a number of reasons for using hidden fields in WFFM:

  • The same form is inserted into multiple pages, because you don’t want to create lots of copies of the same form and maintain them separately (I previously made a fix for the wizard before cos it got annoying). You need to identify the page the user submitted the form on.
  • You want pages of a certain template to always include the form so you add it to the presentation details on the Standard Values of a template. Again, you need to identify submission page.
  • You’ve added the form to an item which handles wildcard items there users will browse the page using multiple URLs. Maybe you also want to include an ID or field from the item.
  • Pass through a value that will be used by your Save Actions, maybe a campaign or originator key.

For example, we have used it to pass along additional properties to a custom Save to Marketo action. Or include details about the page the user was on when they completed the form, maybe including this as a field in the message that gets sent using a Send Email Message save action so we have a little more context as to what they were looking at. Or use it on a commenting save action so we know which article a submission relates to, allowing us to display those on the article.

Hidden Token Field

The field is a pretty standard WFFM custom field, you know the drill:

The token is resolved in the GetTokenValue() method. Once again, this is MVC only, you could easily provide an ASP.Net Webforms implementation if you needed though. You could also make this much more extendable if you need to and make the tokens resolvable via a custom pipeline. We generally tended to have a limited number of tokens with simple logic though.

The custom field is now available to select in the Form Designer and an additional property is available allowing you to select the token value using a drop field:

WFFM Hidden Token Field  - Form Designer

WFFM Hidden Token Field – Form Designer

Except not quite. It’s probably empty at the moment. If you were paying attention then you have may have noticed the following attribute on the TokenValue property:

[VisualFieldType(typeof(ListChoiceField), new object[] { "{7FF10263-1505-48B3-9027-D26FF529D7F1}" })]

The GUID is an Sitecore Item which is the parent of items that contains the possible values for this property, go create a folder under the Meta Data folder and change this guid to your instance values and then create the list of possible options from the Extended List Item template:

Hidden Tokens Meta Data List

Hidden Tokens Meta Data List

The value field of each item should match the token values from code. Now the list will be populated using the items you just created, it’s the same as how the CSS Class field works.

There’s a couple of tokens above worth mentioning:

  • {custom} : This allows the author to add any value they like, e.g. a campaign or originator value. We use this to pass along custom values to Marketo or CRM for example.
  • {fieldname} : This will try to populate the value from a field on the Context Item, which is useful when you’re using the form on multiple pages (such as wildcard items). Maybe you have a product id/name field that you also want to submit with the form so you know what the user was looking at.

When the fields are rendered as hidden input types they will be populated with the values, and submitted along with the form.

Hidden Token Field - Rendered Markup

Hidden Token Field – Rendered Markup

Yes, the value of the hidden can be modified by the client. Don’t use for anything sensitive, defensively code around submitted values and certainly don’t put your Credit Card details in there 😋

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s