Table of Contents

Entity view validation rule

What are entity view validation rules ?

Entity view validation rules are used to check the validity of the properties of an entity view in the server application. They can also be executed on the client side so the user can see errors before clicking on the Save button. They are executed before saving but after the Saving event rules. If an entity view validation rule returns an error, the save is cancelled.

Writing the code of an entity view validation rule

In this article, we will focus on the available properties.

You can either write the C# code directly in the Neos Studio code editor or in Visual Studio (after the validation rule has been saved once). See more about adding/editing server code.

If the rule is configured to run on the client side, it will be transpiled to TypeScript. In this case, the rule must be simple (no calls to external services or methods, no use of class variables or properties). If you encounter transpilation errors, try simplifying your code, using alternative syntaxes or configuring the rule to be executed on server-side only. Keep in mind that a client-side rule can be triggered very often, enable this option only for your simplest rules.

Example with a CheckDiscount entity view validation rule :

/// <inheritdoc/>
public override IValidationRuleResult Execute()
{
    if (Item.Discount < 0 || Item.Discount > 100)
    {
        return Error(GroupeIsa.Northwind.Domain.Properties.Resources.Sales.DiscountMustBeBetweenZeroAndHundred);
    }

    return Success();
}

Item property

The Item represents element being validated.

Returned Result

The ExecuteAsync or Execute methods return a IValidationRuleResult.

You must use one of the following methods to obtain an instance of the result to return :

  • Error(string message) : returns an error result containing the message passed as parameter. This prevents the entity from being saved. -Success() : returns a successful result allowing the element to be saved.

Rules properties

  • Name : name of the entity view validation rule, used to name the corresponding C# class
  • Business assembly : business assembly that will contain the C# class representing the entity view validation rule. The chosen assembly must belong to the entity view validation rule module or one of its children.
  • Asynchronous execution : this option must be checked when you execute asynchronous code. Otherwise, unchecking this option makes the code more readable by preventing the method from returning a "Task" object. If necessary, the signature of the generated method can always be changed afterwards.

Advanced options :

  • Trigger conditions : indicates whether the rule is launched when an item is created, modified and/or deleted.
  • Execute : defines where the rule will be executed. It can either be only executed on the server side, or executed both on the client and the server side (in this case the C# code must be compatible with a transpilation to TypeScript).
  • Properties : list of entity view properties that will display validation errors after the rule is executed.
Note

If you want to enable or disable the entity view validation rule, you can use the switch button next to the name of the validation rule.

Warning

Only validation rules triggered during creation and modification can be executed on the client side.

Renaming the rule

To rename the validation rule, click on the name of the rule or the button. The validation rule is reloaded before opening the modification popup to ensure that any changes made in Visual Studio are taken into account.