LogoLogo
Back to studio
  • 🧠Core Concepts
    • Introduction to Formsort
    • Formsort quickstart guides
      • Add content and collect answers
      • Capture demographic data
      • Add informational content
      • Template your variables
      • Add conditional logic
      • Using conditional logic with Calculated and API variables
      • Add a scheduling option
      • End the flow
      • Review your variable schema
      • Set up integrations
    • How data works in Formsort
      • Responder UUIDs
    • Understanding flows
    • Versioning in Formsort (Deploying)
      • Variant revisions
      • Managing revisions
  • ✨Creating Flows
    • Building a new flow
      • Groups
      • Steps
      • Copy-pasting form content
  • Adding questions and content
    • Questions
      • Select
      • Text
      • Address
      • Comparison
      • Confirmation
      • Date
      • Date & time
      • Email address
      • File upload
      • Grid choice
      • Iframe
      • Image upload
      • Number
      • Payment
      • Phone number
      • Postal code
      • Question group
      • Region
      • Signature
      • SSN
      • Yes/No
    • Content
      • Statement
      • Image
      • Next button
      • Video
      • Divider
      • Map
  • Controlling the flow with conditions and logic
    • Advanced logic
  • Variable templating
  • Redirects and endings
  • Field validation
  • Flow and variant management
  • Content library
  • 🧬JSON Form Definition
  • JSON schemas
  • Validating flow schemas
  • Events subscriptions
  • Flow content data format
  • 🎨Styling
    • Customizing appearance
      • Content area & form layout
      • Buttons
      • Typography
      • UI states
      • Color and dimension variables
      • Question containers
      • Inputs and dropdowns
      • Checkmarks
      • Tables
      • Sliders
      • Divider lines
      • Progress bar
      • Comparison cards
      • Animations and transitions
  • CSS & Advanced Styling
    • Custom CSS overrides
    • Step styling
    • CSS reference
  • 🔁Form Behavior Settings
    • Variant settings
      • Form behavior for returning users
      • Group ranking API
    • Navigation sidebar
  • ⚙️Response Data Collection & Management
    • Schema (variables)
      • Variables from questions
      • Externally provided variables
      • Calculated variables
      • API lookups
      • System Library variables
      • Orphaned variables
  • Saving & retrieving responses
  • Importing Data
    • URL parameters
    • POST body
    • Embed query parameters
  • 📊Analytics and Attribution
    • Built-in analytics
    • Split testing
  • 🚀Publishing and Deployment
    • Live preview overview
    • Environments
      • Loading different environments
    • Embedding
      • Web-embed API
        • React-embed
      • Adding authentication
      • Embedding forms in iOS and Android
      • Setting up a dev environment
    • Pre-deployment checklist
  • 📁Workspace Management
    • Accounts
      • Roles and permissions
    • Custom domains
    • Workspace domain detection
  • 🛠️Formsort Admin API
    • Admin API
  • 🔌Integrations
    • Form answers and events
      • Analytics events
      • Signed requests
      • Event payload shape
      • Submission frequencies
      • Runtime error reporting
    • Integration reference
      • Amplitude
        • Amplitude cross domain tracking
      • BigQuery
      • FullStory
      • Google Analytics
        • Updating from Universal Analytics to GA4
      • Google Cloud Storage
      • Google Sheets
      • Google Tag Manager (GTM)
        • JavaScript triggered by flow events
      • Hubspot
      • Jornaya
      • Optimizely
      • PostgreSQL
      • Redshift
      • Rudderstack
      • S3
      • Salesforce
      • Segment
        • Segment Setup
        • Segment cross domain tracking
      • Stripe
      • TrustedForm
      • Webhooks
        • Zapier
Powered by GitBook
On this page
  • Adding read-only content
  • Setting the size of the container
  • Dynamic setting of size
  • Collecting an answer from within the question
  • Setting an answer value
  • Clearing an answer value
  • Getting the existing value in the frame
  • Accessing other answers already collected

Was this helpful?

  1. Adding questions and content
  2. Questions

Iframe

A custom question, hosted by you within an iframe.

PreviousGrid choiceNextImage upload

Last updated 12 months ago

Was this helpful?

Formsort supports dozens of question types out of the box. However, in some cases it makes sense for you to implement a custom question type yourself using the Formsort .

A great example of this would be an airline ticket purchasing flow, which at one point requires picking a seat (or multiple seats).

While you could use select buttons in a grid to achieve this within Formsort, backed by an API to fetch the choice options, maintaining the logic of which planes are in your fleet and which seats are available would be much more easily done as part of your existing technical infrastructure.

Instead of having to custom engineer the entire form flow, you can build just the single question you need and plug it into your existing flow, reusing the Formsort questions and logic for the remainder.

To view various sample implementations of custom questions, check out this repo:

Adding read-only content

The simplest custom content is read only. Once a user sees the content, they will be allowed to continue along in the flow.

This is useful if you have something like a privacy policy you need to show a user, and you don't want to link to it or have to keep the content within Formsort.

To add it, set the Source URL to the URL of your content, and enable Is read-only?.

You yourself host custom content and questions (or use something like a headless CMS to do so). During development, it's recommended to point the Source URL to localhost and view the flow in the Live preview.

Setting the size of the container

Within the custom question settings, you can set the Default width and Default height.

If your content or question is larger than this size, the default behavior is to allow it to scroll. If you'd rather disallow scrolling, you can set the behavior in overflow-y.

Dynamic setting of size

The <iframe> security model does not allow the containing frame to access your content directly, so Formsort does not know the size of your content if you don't tell it.

For example, this snippet would set the size of the question within formsort to the size of the body.

import { setQuestionSize } from '@formsort/custom-question-api';

window.addEventListener('load', () => {
  const width = document.body.offsetWidth;
  const height = document.body.offsetHeight;
  setQuestionSize(width, height);
})

Take care to only call this when your content has fully loaded or rendered (eg, if you're using React, then on the componentDidMount() class method or useEffect hook), and not to make it too big so that the rest of the step it is embedded in becomes unusable.

Collecting an answer from within the question

Of course, being able to actually collect data and store it is the essence of form building.

Setting an answer value

To set an answer, use the custom question API's setAnswerValue method with the value you want to store. Like answers to other questions in Formsort, this value will be stored at in the answers using the key specified in Answer variable name, you only need to provide the value.

If I just had a simple text input, my setup would look like this:

<input id="my-input" />
import { setAnswerValue } from '@formsort/custom-question-api';

const inputEl = document.querySelector('#my-input'):
inputEl.addEventListener('change', e => {
  setAnswerValue(e.target.value);
})

Formsort is type-aware, make sure to set the Expected answer type to match the type you are returning.

Clearing an answer value

If your custom question has a notion of clearing a value, you can use the clearAnswerValue with no parameters.

Adding a button to clear the input from the previous example, we can add clearing behavior.

<input id="my-input" />
<button id="clear-btn">Clear</button>
import {
  setAnswerValue ,
  clearAnswerValue
} from '@formsort/custom-question-api';

const inputEl = document.querySelector('#my-input'):
inputEl.addEventListener('change', e => {
  setAnswerValue(e.target.value);
})

const clearBtnEl = document.querySelector('#clear-btn'):
clearBtnEl.addEventListener('click', e => {
  inputEl.value = '';
  clearAnswerValue();
})

Getting the existing value in the frame

Sometimes, the user will already have an answer to the question you are asking, either because they are returning to a flow and you have enabled Returning responder behavior to retain their answers, or they have merely clicked back to a previous step when filling out their flow.

To initialize your question with the existing answer, use the getAnswerValue() method. Note that it returns a promise, since <iframe> can only communicate via asynchronous messages.

import { getAnswerValue } from '@formsort/custom-question-api';

getAnswerValue().then(value => {
  // Initialize your question with the existing value.
})

Accessing other answers already collected

If your question needs to know other answers that have already been collected in the flow, you may obtain a promise for the the whole object of answers already collected using getAllAnswerValues(). You can trust that the answers will be of the types configured within Formsort.

import { getAllAnswerValues } from '@formsort/custom-question-api';

getAllAnswerValues().then(answers => {
  // Initialize your question with the existing values

  const birthday = answers['birthday'] // Access answers using their variable names as defined in Formsort.
})

If you're interested in the responder UUID, you may obtain that in a similar way using getResponderUuid(). This can be used for analytics on your end, or to obtain data about this user from your own systems that might not be present within Formsort.

<iframe> elements may communicate with their containing pages (in our case, the Formsort flow) by passing messages. The Formsort faciliates this with a few methods.

For additional details, see the repo on GitHub.

Custom Question API
https://github.com/formsort/custom-question-examples
Custom Question API
Custom Question API