Skip to main content
A newer version of this page is available. .
All docs
V21.1
.NET Framework 4.5.2+

Implement Property Value Validation in Code (EF Core)

  • 2 minutes to read

This lesson explains how to set validation rules for business classes and their properties. XAF applies these rules when a user executes the specified operation (for example, saves an edited object).

In this lesson you will create a rule that requires that the Position.Title property must not be empty. The application will apply the rule when a user saves a Position object.

Note

To use XAF Validation functionality, install the DevExpress.ExpressApp.Validation.Blazor NuGet package and register it.

The project wizard adds this package to all new applications with the enabled security options.

Step-by-Step Instructions

  1. Apply the RuleRequiredFieldAttribute attribute to the Title property in the Position class. As an attribute parameter, specify the context that triggers the rule (for example, DefaultContexts.Save):

    using DevExpress.Persistent.Validation;
    //...
    public class Position {
        //...
        string title;
        [RuleRequiredField(DefaultContexts.Save)]
        public string Title {
            get => title; set {
                if(title == value) {
                    return;
                }
    
                title = value;
                OnPropertyChanged();
            }
        }
    }
    
  2. Run the application.

    Click the New button to create a new Position object. Leave the Title property empty and click Save. The following error message is displayed:

    XAF ASP.NET Core Blazor validation

Detailed Explanation

The RuleRequiredField attribute defines a validation rule that ensures that the Position.Title property has a value when the Position object is saved.

The Validation System offers a number of Rules and Contexts. Refer to the following topic for details: Validation Rules. The Application Model stores all rules so you can add and edit Rules and Contexts with the Model Editor (see the Implement Property Value Validation in the Application Model topic).

Next Lesson

Add a Simple Action

See Also