Creating conditional custom fields

Problem: Fields in Aristotle are always shown and can’t include conditional values (eg. only show if another value is selected.

Feature: We are going to solve this using Custom Fields and JSON Forms in Aristotle

Snippets:

  1. When setting up a custom field, select the Structured Data (JSON) option

  1. Add a JSON Schema for the fields required, including a yes/no field to control the form. eg:
{
  "type": "object",
  "required": [
    "external_custodian_exists"
  ],
  "properties": {
    "external_custodian_exists": {
      "enum": [
        "Yes",
        "No"
      ],
      "type": "string"
    },
    "external_custodian_details": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "phone": {
          "type": "string"
        },
        "position": {
          "type": "string"
        },
        "organisation": {
          "type": "string"
        }
      }
    }
  }
}
  1. Include JSON Schema to conditionally hide or show extra fields:
{
  "type": "HorizontalLayout",
  "elements": [
    {
      "type": "Control",
      "label": "Is there an External Custodian?",
      "scope": "#/properties/external_custodian_exists",
      "options": {
        "format": "radio"
      }
    },
    {
      "rule": {
        "effect": "SHOW",
        "condition": {
          "scope": "#/properties/external_custodian_exists",
          "schema": {
            "const": "Yes"
          }
        }
      },
      "type": "VerticalLayout",
      "elements": [
        {
          "type": "Control",
          "scope": "#/properties/external_custodian_details/properties/name"
        },
        {
          "type": "Control",
          "scope": "#/properties/external_custodian_details/properties/organisation"
        }
      ]
    }
  ]
}
  1. This will provide an editor with optional fields that can be shown or hidden like below:

ezgif-4-de0df1f6fa

1 Like

Thanks Sam.

Is there an option for the fields (i.e. Name and Organisation) to be mandatory if they are shown?

Yes - thats really easy!!

In the JSON Schema section adding a “required” option will make certain fields mandatory - and a user will be prevented from saving an item if its not completed.

    "external_custodian_details": {
      "type": "object",
      "required": ["name", "organisation"]
      "properties": {
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "phone": {
          "type": "string"
        },
        "position": {
          "type": "string"
        },
        "organisation": {
          "type": "string"
        }
      }
   }
1 Like