Set up the requirement condition of a field on a page

Medium
PDF

Example. Make the Description field on the record page of the custom Requests section required. The field must be required if the request is new, i. e., the Status field is set to "New."

1. Set up the page UI 

  1. Create a custom Requests app based on the Records & business processes template. To do this, follow the guide in the user documentation: Create a custom app.
  2. Open the Requests form page page in the working area of the Requests app page.

    The Requests form page page includes the Name field by default.

  3. Add a request status field.

    1. Add a Dropdown type field to the working area of the Freedom UI Designer.
    2. Click in the action panel of the Freedom UI Designer and fill out the field properties in the setup area.

      • Set Title "Status."
      • Set Code to "UsrStatus."
      • Click the button next to the Lookup property and fill out the lookup properties:

        • Set Title to "Request status."
        • Set Code to "UsrRequestStatusLookup."

        Click Save to add the lookup.

    3. Click Save on the Freedom UI Designer's toolbar.
  4. Fill out the Request status lookup.

    1. Open the Requests app page and click Run app.
    2. Click to open the System Designer. Go to the System setup block → Lookups.
    3. If you use Creatio 8.0.0, register the lookup. The lookup is registered automatically in Creatio 8.0.1 and later.

      1. Click New lookup on the Lookups section toolbar and fill out the lookup properties:

        • Set Name to "Request status."
        • Set Object to "Request status."
      2. Click Save on the lookup setup page's toolbar.
    4. Open the Request status toolbar.
    5. Click New on the lookup setup page's toolbar and add the following lookup values:

      • "New"
      • "Under evaluation"
      • "In progress"
      • "Canceled"
      • "Completed"
  5. Add a request description field.

    1. Open the Requests form page page and add a Text type field to the working area of the Freedom UI Designer.
    2. Click in the action panel of the Freedom UI Designer and fill out the field properties in the setup area.

      • Set Title to "Description."
      • Set Code to "UsrDescription."
  6. Click in the action panel of the Freedom UI Designer. After you save the page settings, Creatio opens the source code of the Freedom UI page.

2. Set up the condition that makes the field required 

Configure the business logic in the Client Module Designer. For this example, set up the condition that makes the field required.

  1. Bind the crt.Required type validator to the UsrDescription model attribute in the viewModelConfig schema section. The validator checks the attribute value.

    viewModelConfig schema section
    viewModelConfig: /**SCHEMA_VIEW_MODEL_CONFIG*/{
        "attributes": {
            ...
            "UsrDescription": {
                ...,
                /* The property that enables validators in the attribute. */
                "validators": {
                    /* Flag the field as required. */
                    "required": {
                        "type": "crt.Required"
                    }
                }
            },
            ...
        }
    }/**SCHEMA_VIEW_MODEL_CONFIG*/,
    
  2. Add a custom implementation of the crt.HandleViewModelAttributeChangeRequest system query handler to the handlers schema section. Run the handler when the value of any attribute changes, including changes made after loading the attribute values from the data source. The handler checks the UsrStatus attribute value. If the new attribute value refers to the "New" value of the Request status lookup, apply the validator, otherwise do not apply it. The unique status ID of the new request set as the newStatusId constant is stored in the corresponding column of the Request status lookup's record string. To display the Id column in the Request status lookup list, follow the guide in the user documentation: Work with record lists. In this example, the status ID of the new request is "3be636fa-12b4-40eb-a050-91b1d774a75f."

    handlers schema section
    handlers: /**SCHEMA_HANDLERS*/[
        {
            request: "crt.HandleViewModelAttributeChangeRequest",
            /* The custom implementation of the system query handler. */
            handler: async (request, next) => {
                if (request.attributeName === 'UsrStatus') {
                    const newStatusId = '3be636fa-12b4-40eb-a050-91b1d774a75f';
                    const selectedStatus = await request.$context.UsrStatus;
                    const selectedStatusId = selectedStatus?.value;
                    const isNewRequest = selectedStatusId === newStatusId;
                    /* Check the request status. */
                    if (isNewRequest) {
                        /* If the request is new, apply the required validator to the UsrDescription attribute. */
                        request.$context.enableAttributeValidator('UsrDescription', 'required');
                    } else {
                        /* Do not apply the required validator to the UsrDescription attribute for non-new requests. */
                        request.$context.disableAttributeValidator('UsrDescription', 'required');
                    }
                    /* Call the next handler if it exists and return its result. */
                    return next?.handle(request);
                }
            }
        }
    ]/**SCHEMA_HANDLERS*/,
    
    Complete source code of the page schema
  3. Click Save on the Client Module Designer's toolbar.

Outcome of the example 

To view the outcome of the example:

  1. Open the Requests app page and click Run app.
  2. Click New on the Requests app toolbar.
  3. Enter "Request's name" in the Name field.
  4. Select "New" in the Status field.

As a result, Creatio will make the Description field required for new requests.

The Description field will not be required for requests that have other statuses. For example, "Completed."