Table of Contents

Creating a validation rule

In this article, you will learn how to create a validation rule that checks the validity of a product's unit price. This rule will control that the unit price is greater than 0 when it is filled.

Generating the business assemblies

To be able to write a rule or a server event, it is first necessary to have created the C# projects associated with the module, also called business assembly. To learn more about server code, you can check out this article.

Open the Catalog module in Neos Studio, go to Business assemblies tab and press the Create solution button. You should have now :

- Northwind.Catalog.Application
- Northwind.Catalog.Domain

This action generates four C# projects. One for the Domain layer and another for the Application layer and their unit test projects:

Catalog
└─── businessAssembly
     └─── Application
          |    Catalog.Application.csproj
     └─── Application.Tests.UnitTests
          |    Catalog.Application.Tests.UnitTests.csproj
     └─── Domain
          |    Catalog.Domain.csproj
     └─── Domain.Tests.UnitTests
          |    Catalog.Domain.Tests.UnitTests.csproj

Creating the validation rule metadata

Add a validation rule with the following properties on the ProductView entity view:

Name = UnitPriceValidation

Click on the advanced options button to edit the validation rule:

Execute = Client-side and server-side
Properties = UnitPrice

You will use the Code property to define in C# the validation of the view by replacing the content of the Execute method.

/// <inheritdoc/>
public override IValidationRuleResult Execute()
{
    if (Item.UnitPrice != null && Item.UnitPrice <= 0)
    {
        return Error(Resources.Catalog.UnitPriceMustBeNullOrGreaterThanZero);
    }

    return Success();
}

The rule will always be executed on the server before saving. When the Execute property is set to Client-side and server-side, the rule will be transpiled in TypeScript and executed in your web browser right after setting the price of a product without calling the server.

Save the changes and generate the application.

Click on the Application generation button :

or the command

neos generate

The content of your validation rule is stored in a C# class in the business assembly's Application layer module. It is accessible via the Visual Studio button present above the editor code.

Catalog
└─── businessAssembly
     └─── Application
          └─── ValidationRules
               └─── ProductView
                    |    UnitPriceValidation.cs

Testing the validation rule

Open the application with the https://localhost/neos/Northwind URL.

You can test the rule in the product edit page. The validation rule should be triggered when you edit the UnitPrice property (client-side) and at saving (server-side). However you will not be able to test the server-side validation this way because the client will not send a save request to the server when a validation error is present.

Advanced validation rule with TDD method

For an example of validation rule implementation following the TDD method, you can refer to this article.