学习008-02-05-02 Implement Property Value Validation(实现属性值验证)

Implement Property Value Validation(实现属性值验证)

This lesson explains how to set validation rules for entity classes and their properties. XAF applies these rules when a user executes a specific operation (for example, saves an edited object).
本课介绍如何为实体类及其属性设置验证规则。当用户执行特定操作(例如,保存已编辑的对象)时,XAF应用这些规则。

The Validation System offers a number of predefined rule types and contexts. A rule specifies a data validity condition. A context defines the moment when the application checks the rule.
验证系统提供了许多预定义的规则类型和上下文。规则指定数据有效性条件。上下文定义应用程序检查规则的时刻。

To use XAF Validation functionality, you need the DevExpress.ExpressApp.Validation.Blazor and DevExpress.ExpressApp.Validation.Win NuGet packages. When you created the solution and selected the Validation module in the project wizard, the wizard added these packages to platform-specific projects.
要使用XAF验证功能,您需要DevExpress. ExpressApp.Validation.Blazor和DevExpress.ExpressApp.Validation.WinNuGet包。当您创建解决方案并在项目向导中选择验证模块时,向导会将这些包添加到特定于平台的项目中。

Tip
To add this functionality to an existing application, you need to install the NuGet packages and register the modules. For additional information, refer to the following section: Validation System Elements.
要将此功能添加到现有应用程序,您需要安装NuGet包并注册模块。有关其他信息,请参阅以下部分:验证系统元素。

The instructions below explain how to do the following:
以下说明解释了如何执行以下操作:

  • Use the Model Editor to prevent a user from marking a task as completed before the task starts (DemoTask.Status is NotStarted).
  • 使用模型编辑器防止用户在任务开始之前将任务标记为已完成(DemoTask. status is NotStarted)。
  • Create a rule in code that requires that the Position.Title property must not be empty.
  • 在代码中创建要求Position. Title属性不得为空的规则。

Implement Property Value Validation in Model Editor(在模型编辑器中实现属性值验证)

1.Open the Model.DesignedDiffs.xafml file in the Model Editor. Navigate to the Validation | Rules node. Right-click the Rules node and select Add… | RuleCriteria.
在模型编辑器中打开Model. DesignedDiffs.xafml文件。导航到验证|规则节点。右键单击规则节点并选择添加…|规则标准。

在这里插入图片描述

2.Set values of the following properties for the node:
为节点设置以下属性的值:

  • TargetType to MySolution.Module.BusinessObjects.DemoTask
  • Criteria to Status != ‘NotStarted’
  • ID to TaskStarted
  • TargetContextIDs to MarkCompleted
  • CustomMessageTemplate to Cannot set the task as completed because it has not started.
    在这里插入图片描述

The Criteria property value must respect the Criteria Language Syntax. To set the criteria, click the ellipsis button to the right of the Criteria value and invoke the Filter Builder dialog. In this dialog, you can visually design a criteria expression.
标准属性值必须符合标准语言语法。要设置标准,请单击标准值右侧的省略号按钮并调用过滤器生成器对话框。在此对话框中,您可以直观地设计标准表达式。

3.Navigate to the ActionDesign | Actions | DemoTask.MarkCompleted node and set the ValidationContexts property to MarkCompleted.
导航到ActionDesign|Actions|DemoTask. MarkCompled节点并将ValidationContext属性设置为MarkCompled。
在这里插入图片描述

4.Change the caption of the MarkCompleted Action to Mark Completed.
将标记已完成操作的标题更改为标记已完成。

5.Run the application and go to one of the unfinished tasks. Click the Mark Completed button. The following Validation Error dialog appears:
运行应用程序并转到其中一个未完成的任务。单击标记已完成按钮。出现以下验证错误对话框:

ASP.NET Core Blazor
在这里插入图片描述

Windows Forms
在这里插入图片描述

Implement Property Value Validation in Code( 在代码中实现属性值验证)

1.Apply the RuleRequiredField attribute to the Title property in the Position class. Specify the context that triggers the rule (for example, DefaultContexts.Save) as the attribute’s parameter:
将Rule必需字段属性应用于Poplace类中的Title属性。指定触发规则的上下文(例如,DefaultContexts.Save)作为属性的参数:

C#

// ...
using DevExpress.Persistent.Validation;

//...
{
    [DefaultClassOptions]
    [DefaultProperty(nameof(Title))]
    public class Position : BaseObject
    {
        /*Define a validation rule that ensures that the `Position.Title` property
          has a value when you save the `Position` object.*/
        [RuleRequiredField(DefaultContexts.Save)]
        public virtual string Title { get; set; }
        //...
    }
}

2.Run the application and go to the Position List View.
运行应用程序并转到位置列表视图。

Click the New button to create a new Position object. Leave the Title property empty and click Save. The following Validation Error dialog appears:
单击新建按钮以创建一个新的位置对象。将标题属性留空,然后单击保存。出现以下验证错误对话框:

ASP.NET Core Blazor
在这里插入图片描述

Windows Forms
在这里插入图片描述

Next Lesson(下一课)

Highlight Property Editors
突出显示属性编辑器

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值