Skip to content

Logic

You can add logic to a form by creating conditional formattings, data validations or calculated expressions. They are all rules, and the Logic tab at the bottom of the form editor lists them on the left, each with a badge that shows its kind.

Conditional formattings perform actions on the view of your form. You can hide or show elements, change their style, disable them and more.

Data validations check a condition that you define and inform the user accordingly.

Calculated expressions calculate something in the background and show the result on your form.

To add a rule, click the + button at the bottom of the list and choose New Calculated Expression, New Data Validation or New Conditional Formatting.

The add rule menu

Conditional Formattings

When Studio creates a form for a class, it adds a conditional formatting for you. In ProductForm it is named HideIfNew. It hides the Delete button when the form is opened by the Add action, because a new product cannot be deleted.

The HideIfNew conditional formatting of ProductForm

A conditional formatting has these parts:

  1. The If section holds the condition under which the formatting is applied. It is a function that returns a boolean. In the example, Controller.Add.IsActive() is true when the Add action is the one that opened the form.
  2. The Then and Else sections hold the actions. The Then actions run when the condition returns true, and the Else actions run when it returns false.
  3. The Properties panel on the right holds the name of the rule, its Rule Context (select one if the rule should run inside a grid, for example), and its Evaluate on events:

    • Load: the rule is evaluated when the form loads
    • Submit: the rule is evaluated when you save your model
    • Change: the rule is evaluated each time you make a change in your form

To add an action, click Add... in the Then or Else section. Choose its type from the list at the left of the action (in the example, Hide). Then choose the controls that it applies to: click the + sign on the right side of the action and select the controls in the window that opens. The example applies Hide to the control cmdDeleteProduct.

The link Create Reverse Action... creates the opposite action in the other section for you, for example Show in Else for a Hide in Then.

Example

You can hide a control if the length of a collection of the model is equal to 0, with the condition:

function bool Condition()
{
    return Model.Product.Performers.Length == 0;
}

Then add a Hide action in Then, and the reverse action, Show, in Else.

Data Validations

A data validation shows a message when its condition is true. Create one with New Data Validation:

A new data validation, named PriceMustBePositive

  1. The If section holds the condition. It is a function that returns a boolean. The message is shown when it returns true.
  2. In the Properties panel you can change the name of the validation, set its Rule Context (for example, inside a grid), and assign a Validation Group (see Validation). The option Run only before actions that require a Valid Form has a note under it: the rule is evaluated after any form change, after any action execution, and before the actions that require a valid form run.
  3. The Then section holds the Message Type, which is Information, Warning or Error, and the message. A new validation points to a resource that Studio creates in the form's localization, such as LocalResources.RES_DATAVALIDATION_MESSAGE_PriceMustBePositive. Open the resource to write the text.
  4. Apply To lists the controls that the message is attached to. Click the + sign, then click the controls in the form:

Selecting the controls of a data validation

The Then and Apply To sections of the validation

Example

You can show a warning if no performers are selected, meaning that the length of Performers is zero:

function bool Condition()
{
    return Model.Product.Performers.Length == 0;
}

Set the Message Type to Warning, write the message, and choose the control, for example a picklist, in Apply To. When no performer is selected, the message is displayed when the form is saved.

The validations of a form are also listed in the Validation panel. See Validation.

Calculated Expressions

A calculated expression is a function whose result is applied to a property of a control. Create one with New Calculated Expression. The function that Studio creates is a template: it returns an empty string when its condition is true, and null, which leaves the value unchanged, otherwise.

A new calculated expression

  1. The Expression section holds the function. The result is applied to the controls that you choose.
  2. Apply To holds the controls to which the result is applied.
  3. The Properties panel holds the name, the Rule Context, and the Events that evaluate the rule. All three are checked for a new expression:

    • Load: the rule is evaluated when the form loads
    • Submit: the rule is evaluated when you save your model
    • Change: the rule is evaluated each time you make a change in your form

Example

You can show the number of selected items in a label.

function string CalculatedExpression()
{
    int selectedPerformers = Model.Product.Performers.Length;
    if (selectedPerformers == 0) {
        return "(None Selected)";
    }
    else {
        return "(" + selectedPerformers + " Performer(s) Selected)";
    }
}

Choose the label in Apply To. When two performers are selected, the label shows (2 Performer(s) Selected).

Calculated expressions run in the browser

Calculated expressions, like conditional formattings, are compiled into the client. They can use the data of the form but cannot call your Domain helpers or query the database. See Behaviour to know.