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 (XPO)

  • 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 the Validation module in your project.

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 a parameter, specify the context that triggers the rule (for example, DefaultContexts.Save):

    using DevExpress.Persistent.Validation;
    //...
    [DefaultClassOptions]
    [System.ComponentModel.DefaultProperty(nameof(Title))]
    public class Position : BaseObject {
       //...
       private string title;
       [RuleRequiredField(DefaultContexts.Save)]
       public string Title {
          get { return title; }
          set { SetPropertyValue(nameof(Title), ref title, value); }
       }
    }
    
  2. Run the application.

    Click the New button to create a new Position object. Leave the Title property empty and click Save. The 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. For details, refer to the Validation Rules topic. The Application Model stores all rules so that an admin 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