Theme an Advanced Registration Form

On this page

A theme controls how the hosted login pages render an advanced registration form. In this guide, you add readable labels, placeholders, and error messages to the form from the Advanced Registration Forms Tutorial. You manage the theme messages with the API, so you can keep the messages in version control. You can make the same changes in the Admin UI.

For everything else a theme can change, see Themes.

Prerequisites#

  • The real estate registration form and Application from the tutorial.
  • FusionAuth at http://localhost:9011. When FusionAuth runs elsewhere, change the URLs in the scripts.
  • jq and Python 3 on your computer.

Create a Theme#

The default theme is read-only, so create a copy. FusionAuth assigns themes per Tenant. You can change the theme of the Default TenantThe Tenant that FusionAuth creates during installation. You can't delete the Default Tenant, which hosts the FusionAuth Admin UI Application., or create a new Tenant with a new theme. This guide changes the theme of the Default Tenant.

  1. Go to Settings -> API Keys and add an API key. Allow at least every method on the /api/theme endpoint.

  2. Go to Customizations -> Themes and duplicate the default FusionAuth theme. Name the copy Real Estate Application, and save the theme.

  3. Copy the Id of the new theme from the list of themes. The Id is a UUID, such as 42968bbf-29af-462b-9e83-4c8d7c2d55cf.

  4. Go to Tenants and edit the Default Tenant with . On the General tab, set Theme to Real Estate Application, and save the Tenant.

    The Theme setting on the Default Tenant, with the new theme selected.
    The Theme setting on the Default Tenant, with the new theme selected.

Add Labels, Placeholders, and Error Messages#

The theme messages hold placeholders, labels, validation errors, and other text for users. FusionAuth stores the messages in Java properties format in the defaultMessages field of the theme. The default theme renders a label and a placeholder for each field. Both come from the message whose key matches the field key, such as registration.data.minprice. When a theme has no message for a key, the form shows the key.

The following scripts download and upload the messages. The scripts are also in the theme management repository on GitHub.

  1. Save the following script as convert.py. The script turns the escaped newlines from the API into real newlines:

    import sys
    
    OUTPUT = sys.stdin.read()
    formatted_output = OUTPUT.replace('\\n', '\n')
    print(formatted_output)
    
  2. Download the messages to defaultmessages.txt. Replace <your api key> with your API key and <your theme id> with the theme Id:

    $ API_KEY=<your api key> && \
    THEME_ID=<your theme id> && \
    curl -H "Authorization: $API_KEY" 'http://localhost:9011/api/theme/'$THEME_ID|jq '.theme.defaultMessages' |sed 's/^"//' |sed 's/"$//' |python3 convert.py > defaultmessages.txt
    

    The command downloads only the messages. You can extend the command to download other theme fields. The file has about 200 lines and starts like the following excerpt:

    #
    # Copyright (c) 2019-2020, FusionAuth, All Rights Reserved
    #
    # Licensed under the Apache License, Version 2.0 (the \"License\");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #   http://www.apache.org/licenses/LICENSE-2.0
    
    # ...
    
    # Webhook transaction failure
    [WebhookTransactionException]=One or more webhooks returned an invalid response or were unreachable. Based on your transaction configuration, your action cannot be completed.
    
  3. Open defaultmessages.txt in a text editor, and find the section that starts with # Custom Registration forms. Add a message for each field. Each key must match the key of a Form Field:

    # ...
    user.firstName=Your first name
    user.mobilePhone=Your mobile phone num
    registration.data.minprice=Minimum home price
    registration.data.maxprice=Maximum home price
    registration.data.geographicarea=Where are you looking?
    # ...
    

    The file isn't an .ini file, so FusionAuth ignores the sections. Keep your changes small and in place anyway. FusionAuth updates add new messages, and you must merge the new messages into your copy.

  4. Find the section that starts with # Custom Registration form validation errors, and add error messages for the price fields:

    [invalid]registration.data.minprice=Please enter a number
    [invalid]registration.data.maxprice=Please enter a number
    [missing]registration.data.minprice=Minimum home price required
    [missing]registration.data.maxprice=Maximum home price required
    

    An error message key has the form [errortype]fieldkey. The Default validation errors section lists the error types. The price fields have the Number data type. FusionAuth returns invalid when the value isn't a number, and missing when the value is empty. For every error code, see User Data Validation.

  5. When a message contains a double quote, escape the quote as \". The upload script sends the file as a JSON string, and an unescaped quote makes the API call fail.

  6. Save the following script, and run the script in the directory with defaultmessages.txt. The script converts the file to JSON and updates the defaultMessages field of the theme:

    API_KEY=<your api key>
    THEME_ID=<your theme id>
    
    FILE_NAME=out.json$$
    
    awk '{printf "%s", $0"\\n"}' defaultmessages.txt |sed 's/^/{ "theme": { "defaultMessages": "/' | sed 's/$/"}}/' > $FILE_NAME
    
    STATUS_CODE=`curl -XPATCH -H 'Content-type: application/json' -H "Authorization: $API_KEY" 'http://localhost:9011/api/theme/'$THEME_ID -d @$FILE_NAME -o /dev/null -w '%{http_code}' -s`
    
    if [ $STATUS_CODE -ne 200 ]; then
      echo "Error with patch, exited with status code: "$STATUS_CODE
      exit 1
    fi
    
    rm $FILE_NAME
    

To confirm the change, open the registration URL in a private browser window. The fields show the new labels and placeholders:

The first registration step, with a label and a placeholder on each field.
The first registration step, with a label and a placeholder on each field.

Submit the second step with an empty price to see the new error message.

Change How Fields Render#

The customField FreeMarker macro in the theme builds the HTML for each field. The macro checks the control and data type of each field and renders the matching element. For example, a field with the Password control renders as an input element of type password.

To change the HTML of a field, go to Customizations -> Themes , edit your theme, and open Helpers . The customField macro is near the end of the template. The macro builds the element Id from the field key and replaces each period with an underscore. The form processing logic expects that Id format.

You can use FreeMarker, message bundles, CSS, and JavaScript to customize and localize the pages. For more information, see Advanced Themes.

Add a Hidden Field#

A hidden field captures data that the user doesn't type, such as a referrer.

  1. Create a Form Field for the value, such as user.data.referrer with the Text control.

    The Add Form Field page for a hidden field.
    The Add Form Field page for a hidden field.
  2. Add the field to the registration form.

  3. In the customField macro in Helpers , render a hidden input for the field key:

    Generating a hidden field
    [#if field.key == "user.data.referrer"]
      <input id="user_data_referrer" type="hidden" name="user.data.referrer">
    [/#if]
    

    Keep the macro from rendering the visible input for the same key. The Id replaces each period in the key with an underscore, as the form processing logic expects.

  4. Set the value of the field with JavaScript in the theme:

    Setting a hidden field
    document.getElementById("user_data_referrer").value = "value";
    

To confirm the result, register a user and open the User data tab of the user in the Admin UI. The tab shows the referrer value.