Back to Devexpress

Implement Property Value Validation in Code (EF 6)

expressappframework-113638-getting-started-in-depth-tutorial-winforms-webforms-business-model-design-business-model-design-with-entity-framework-6-implement-property-value-validation-in-code-ef.md

latest4.1 KB
Original Source

Implement Property Value Validation in Code (EF 6)

  • Jul 07, 2021
  • 4 minutes to read

This lesson explains how to set rules for business classes and their properties. These rules are validated when an end-user executes a specified operation. This lesson will guide you through implementation of a rule that requires that the Position.Title property must not be empty. This rule will be checked when saving a Position object. You will also be able to see the user interface elements that report a broken rule.

Note

Before proceeding, take a moment to review the following lessons:

  • Inherit from the Business Class Library Class (EF 6)

  • Implement Custom Business Classes and Reference Properties (EF 6)

  • The validation functionality is provided by the Validation Module. Add this module to your MySolution.Module project. For this purpose, find the Module.cs (Module.vb) file in the MySolution.Module project displayed in the Solution Explorer. Double-click this file to invoke the Module Designer. In the Toolbox , navigate to the DX.21.2: XAF Modules section. Drag the ValidationModule item from this section to the Designer’s Required Modules panel. Rebuild your solution.

  • Apply the RuleRequiredFieldAttribute attribute to the Position class’ Title property. As a parameter, specify the context for checking the rule (e.g., DefaultContexts.Save). The RuleRequiredField attribute defines a validation rule that ensures that the Position.Title property has a value when the Position object is saved. The following code demonstrates this attribute.

  • Run the WinForms or ASP.NET Web Forms application. Click the New ( ) button to create a new Position. Leave the Title property empty and click the Save button. The following error message will be displayed, depending on the application type.

  • In the WinForms application, close the window with the warning message, set a value for the Title property and click the Save button. In the ASP.NET Web Forms application, set a value for the Title property and click the Save button. The object will be saved successfully.

Note

The Validation System provides a number of Rules and Contexts. For details, refer to the Validation Rules topic. Information on Rules applied in code is loaded into the Application Model (see the Implement Property Value Validation in the Application Model topic). This allows a business application administrator to add and edit Rules and Contexts via the Model Editor.

You can see the code demonstrated here in the MySolution.Module | Business Objects | Contact.cs (Contact.vb) file of the EF Demo (Code First) installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 21.2\Components\eXpressApp Framework\EFDemoCodeFirst.

Next Lesson: Add a Simple Action