Creating your own forms component

This article explains how to create a custom fan-facing form component.

Introduction

The Cortex Forms product includes a fan-facing front end component, with customisable styling, which can be embedded into any website or app using a brief javascript embed code, as explained in How to embed a form. However, there may be a need for something more bespoke, so this article explains how to create a custom forms component.

Endpoints

Base URL (production-standard): https://forms.cortextech.io

Base URL (production-USA): https://forms.cortextech.us

General form actions:

/v1/{clientId}/forms

MethodDescriptionParameters
GETGet a list of forms for a given clienttags, publish from, publish to, linked IDs

Actions relating to a specific form:

/v1/{clientId}/forms/{id}

MethodDescriptionParameters
GETGet a specific formN/A
POSTSubmit answers to a formN/A

Response

Base

KeyData TypeRequiredNotes
idstringYUnique identifier per form, generated by the backend on form creation.
clientIdstringYUnique identifier for the client, as per client in Cortex.
titlestringYThe title of the form.
descriptionstringYSpace to write a more detailed description of the form.
languagestringYRepresents the language in which the form is created.
publishFromstring? (rfc3339)NThe date the form should be "published" from.
publishTostring? (rfc3339)NThe date the form should be available to. Any submitted answers will not be saved from this point onward.
tagsstring arrayYTags associated with the form, can be used for searching.
linkedIdslinkedId array (see below)YA list of associated IDs related to the form, identical to other Cortex systems, this will be an ID relating an external data provider such as Opta, and will allow for searching.
createdDetailseditor (see below)NInformation relating to the Cortex user that created the form. Generated by backend on form submission.
lastEditededitor (see below)NInformation relating to the Cortex user that last edited the form. Generated by backend on form submission.
sectionsfield array (see below)YFields are grouped by section, a user must fill in one section's fields before they can see the next. Currently, there will only be one section, and it is not possible to create additional sections via Cortex. This has been included to accommodate potential future development, which at time of writing is not scheduled.
heroMediaheroMedia (see below)NContent block to render at the top of the form. Follows the same logic as hero content in other Cortex systems.
successsuccess (see below)YContains information about what actions to perform once the form has been submitted.
displayOptionsdisplay Options (see below)YDisplay options for fan-facing forms.
submitButtonsubmitButtonNRepresents the submit button configuration for fan-facing forms.
themeThemeNRepresents the theme.

linkedId

KeyData TypeRequiredNotes
textstringYTextual representation of the linked ID (used for rendering in Cortex)
sourceSystemstringYThe data source of the linked ID, e.g. OPTA_FOOTBALL = football team from Opta's feeds
sourceSystemIdstringYThe ID of the linked item in the data source's system

editor

Note due to privacy, the only information returned from the API will be the date field, all other fields will not be displayed and are purely for internal use.

KeyData TypeRequiredNotes
datedatetimeYThe date this user interacted with this object. NOTE- this is the only field that is visible via the API, all other fields are internal only.

field


KeyData typeRequiredNotes
idstringYEach field needs a unique key in order to link up the conditional logic shown later. This will need to be defined by frontend.
indexuintYOne-based (i.e. starting from one) indexing of fields, which persists across sections.
labelstringYThe title of the field, should be the question.
descriptionstringNThe description of the field, should be some form of elaboration on the question
linkUrlstringNIf the field has an external link associated with it, the URL will be provided here
linkTextstringNIf the external link is to wrapped in text, it will be provided here
typestringYSpecifies the type of input to display to the user. When the type includes subtypes, both the type and subtype are provided. The subtype should be used to determine the specific field type to present to the user.
subtypestringY (if the type has a subtype)
metadatametadata (see below)NMetadata relating to the data capture

type

Field types

Subtype

Description

Notes

shorttext

n/a

Represents a short text input field, typically with a limited character count.

longtext

n/a

Represents a multi-line text input field, suitable for longer descriptions or comments.

client_preference

n/a

Represents a field for capturing client preferences, which could vary in format based on specific requirements or context.

Preferences, and their options, are set centrally in the Cortex platform outside of Forms. The preferences service has use cases in other Cortex products, particularly SSO. Hence, the same preference questions can be displayed in a form as in an SSO registration.

choice

Represents a field for providing a selection of choices or options, allowing users to pick one or more selections from a predefined list.

checkbox

Represents a checkbox input field, allowing users to select multiple options.

radio

Represents a radio button input field, allowing users to select only one option from a list.

select

Represents a dropdown select input field, allowing users to choose one option from a list.

account_detail

Represents a field for capturing detailed information related to a fan’s account or contact information.

Note: If an authentication token is present, any submitted values associated with these field types will be overwritten by the corresponding values in the token claims. Therefore it is recommended to prefill and disable or hide these fields if a token is present.

email

Represents a field for entering an email address.

first_name

Represents a field for entering a first name.

last_name

Represents a field for entering a last name.


metadata

KeyData typeRequiredNotes
valueDepends on field type (see below)N(see below)
validationvalidation (see below)NLogic relating to validating the input
conditionsnode array (see below)NIf the rendering of this field is reliant on an answer provided in a previous question, the conditions are stored here.

validation

KeyData typeRequiredNotes
requiredbooleanNtrue indicates an answer is required for this field
minDepends on field type. (see below)N(see below)
maxDepends on field type. (see below)N(see below)
regexPatternstringNShould a regular expression pattern be applied to the submitted input? If so, the regex can be entered in Cortex and will be provided here.
typestringN(see possible values below)

Type validation rules

Key

Notes

Examples

email

A syntactically valid email address is expected.

[email protected]

number

A valid number is expected. Decimals (the radix character must be a period), positive and negative numbers are all considered valid. Thousand separated numbers are not supported.

1234 +1234 -1234 1.234

phone

A phone number, in E.164 international format, is expected.

+442031379873 +4420 3137 9873 +44(0)2031379873

Per field type validation rules

Field typesSubtypesValidation rulesValidation data typeUser input data typeNotes
shorttext, longtextn/amin, maxintstringRequire the provided string to be equal, less than or greater than the minimum and maximum values respectively in length.
choicecheckbox, selectmin, maxarrayarray (equal to or a subset of the metadata value)Require a minimum or maximum number of selected choices.
account_detailemailstringExpects a syntactically valid email address.
client_preferencen/aintExpects a valid preference ID

Validation error codes


Error codeField typesDescription
invalid_typeAllValue does not meet expected data type.
invalid_valuechoice (any subtype)If validating against an array of values (e.g. possible checkbox options), returned if the value(s) are not contained in the submitted array.
missing_required_fieldAllRequired field is missing a value, or is an empty array or an empty string.
min_length_requiredshorttext, longtext, choice (where subtype is checkbox)If value is a string, the value is less than metadata.validation.min in length. If the value is an array, the value has less than metadata.validation.min elements.
min_value_requiredshorttext (where validation type is set to number)Number value is less than metadata.validation.min.
max_length_exceededshorttext, longtext, choice (where subtype is checkbox)If value is a string, the value is greater than metadata.validation.max in length. If the value is an array, the value has greater than metadata.validation.max elements.
max_value_exceededshorttext, where validation type is set to numberNumber value is greater than metadata.validation.max.
regex_pattern_match_errorshorttextString value does not match regex pattern.
invalid_email_addressshorttext (where validation type is set to email), account_detail (where subtype is email)String value is not a syntactically valid email address.
invalid_phone_numbershorttext (where validation type is set to phone)String value is not a valid international phone number.

node

Key

Data type

Required

Notes

type

string

Y

Indicates the action this node should perform, current values: AND: all child nodes should return true OR: only one of the child nodes should return true EVAL: evaluate this node directly (no need to drill down to children)

key

string

N

If EVAL this should be the key of the related field previously defined.

value

object

N

If EVAL this should be the expected value of the field. Note this can be a string, integer, boolear or array of items.

operator

string

N

If EVAL the comparator operation to perform between key and value.

Example conditions:

  1. Answer to previous question is "Vegetarian". In this case, if the user has supplied "Vegetarian" as the answer of question 1, render this question:
    {
      "conditions": [
        {
          "type": "EVAL",
          "key": "question1",
          "value": "Vegetarian",
          "operator": "EQUAL"
        }
      ]
    }
  2. OR example answer to previous question is Vegetarian OR Vegan.
    {
      "conditions": [
        {
          "type": "OR",
          "nodes": [
            {
              "type": "EVAL",
              "key": "question1",
              "value": "Vegetarian",
              "operator": "EQUAL"
            },
            {
              "type": "EVAL",
              "key": "question1",
              "value": "Vegan",
              "operator": "EQUAL"
            }
          ]
        }
      ]
    }
  3. AND example the answer to question 1 must be Vegetarian and the answer to question 2 must be Vegan
    {
      "conditions": [
        {
          "type": "AND",
          "nodes": [
            {
              "type": "EVAL",
              "key": "question1",
              "value": "Vegetarian",
              "operator": "EQUAL"
            },
            {
              "type": "EVAL",
              "key": "question2",
              "value": "Vegan",
              "operator": "EQUAL"
            }
          ]
        }
      ]
    }

heroMedia

KeyData TypeRequiredNotes
contentcontentNcontentType may be IMAGE or VIDEO. If contentType: "IMAGE", imageUrl will be provided. If contentType: "VIDEO", then sourceSystem: "YOUTUBE", link and sourceSystemId will all be provided.

success

KeyData TypeRequiredNotes
actionstringYThe action to perform when the form is successfully submitted, can be: content, link.
linkUrlstringNThe URL to redirect the user to if action is defined as "link", or to display as a link if action is "content".
linkTextstringNThe link text to display if action is defined as "link" and a linkUrl is provided.
paragraphstringNDisplay a success message on submission.
contentcontentNIf the action is "content", the content block to render. This is the standard content block model used elsewhere in Cortex systems. Currently, possible contentTypes include "IMAGE" and "VIDEO".

displayOptions

KeyData TypeRequiredNotes
hideTitlebooleanNFlag to toggle title visibility in fan-facing forms.

submitButton

FieldTypeRequiredDescription
labelstringNText to display as the submit button e.g. "Enter competition".

theme

FieldTypeDescription
titleFontFontSee explanation below.
captionFontFontSee explanation below.
captionFontColorstringValue provided as a hexadecimal code e.g. #000000".
backgroundColorstringValue provided as a hexadecimal code e.g. "#000000".
backgroundBorderColorstringValue provided as a hexadecimal code e.g. "#000000".
backgroundRadiusstringValue can be one of "SHARP", or "SMOOTH"
buttonFontFontObject including name, size, url, and allCaps. See explanation below.
buttonFontColorstringValue provided as a hexadecimal code e.g. "#000000".
buttonBackgroundColorstringValue provided as a hexadecimal code e.g. "#000000".
buttonBorderColorstringValue provided as a hexadecimal code e.g. "#000000".
buttonRadiusstringValue can be one of "SHARP", "SMOOTH", or "PILL"
questionLabelFontFontSee explanation below.
responseFontFontSee explanation below.
selectedField ColorstringValue provided as a hexadecimal code e.g. "#000000".
unselectedField ColorstringValue provided as a hexadecimal code e.g. "#000000".
errorColorstringValue provided as a hexadecimal code e.g. "#000000".
questionLabelFontColorstringValue provided as a hexadecimal code e.g. "#000000".
responseFontColorstringValue provided as a hexadecimal code e.g. "#000000".

Font

FieldTypeDescription
namestring
sizeinteger
urlstring
allCapsboolean

Answer

Base

KeyData TypeRequiredNotes
fieldsfield array (see below)YThe answers for each field are stored here.

field

KeyData TypeRequiredNotes
idstringYThe key of the field when the form was generated.
valuestringYThe submitted response to the field, by the user.