Implement Property Value Validation in Code 在代码中实现属性值验证(XPO)
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.
本课介绍如何为业务类及其属性设置规则。当最终用户执行指定的操作时,将验证这些规则。本课将指导您完成一个规则的实施,该规则要求位置.Title 属性不能为空。保存"位置"对象时将检查此规则。您还可以看到报告损坏规则的用户界面元素。
Note注意
Before proceeding, take a moment to review the following lessons.
- Inherit from the Business Class Library Class (XPO)
- Implement Custom Business Classes and Reference Properties (XPO)
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.19.2: XAF Modules section. Drag the ValidationModule item from this section to the Designer's Required Modules panel. Rebuild your solution.
在继续之前,请花点时间复习以下课程。
从商务舱库类 (XPO) 继承
实现自定义业务类和参考属性 (XPO)
验证功能由验证模块提供。将此模块添加到 MySolution.模块项目中。为此,在解决方案资源管理器中显示的 MySolution.模块项目中查找Module.cs(Module.vb)文件。双击此文件以调用模块设计器。在工具箱中,导航到 DX.19.2:XAF 模块部分。将验证模块项从此部分拖动到"设计器的必需模块"面板。重建解决方案。

In a WinForms application, add the ValidationWindowsFormsModule. This module creates validation error messages that are more informative and user friendly than the default exception messages. Additionally, this module provides in-place validation support (see IModelValidationContext.AllowInplaceValidation). To add this module, find the WinApplication.cs (WinApplication.vb) file in the MySolution.Win project displayed in the Solution Explorer, double-click this file to invoke the Application Designer and drag the ValidationWindowsFormsModule from the Toolbox to the Required Modules panel.
在 WinForms 应用程序中,添加验证窗口窗体模块。此模块创建比默认异常消息更丰富、更友好的验证错误消息。此外,此模块还提供就地验证支持(请参阅 IModel 验证上下文.允许位置验证)。要添加此模块,请在解决方案资源管理器中显示的 MySolution.Win 项目中查找WinApplication.cs (WinApplication.vb) 文件,双击此文件以调用应用程序设计器,并将验证Windows窗体模块从工具箱拖动到"必需模块"面板。
In an ASP.NET application, you can also add the ValidationAspNetModule. This module provides in-place validation support (see IModelValidationContext.AllowInplaceValidation). To add this module, find the WebApplication.cs (WebApplication.vb) file in the MySolution.Web project displayed in the Solution Explorer, double-click this file to invoke the Application Designer and drag the ValidationAspNetModule from the Toolbox to the Required Modules panel.
在ASP.NET应用程序中,还可以添加验证AspNet模块。此模块提供就地验证支持(请参阅 IModel 验证上下文.允许位置验证)。要添加此模块,请在解决方案资源管理器中显示的 MySolution.Web 项目中查找WebApplication.cs (WebApplication.vb) 文件,双击此文件以调用应用程序设计器,并将验证AspNet模块从工具箱拖动到所需的模块面板。
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 following code demonstrates this attribute.
将"规则所需字段属性"属性应用于职位类的"标题"属性。作为参数,指定用于检查规则的上下文(例如,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); }
}
}The RuleRequiredField attribute defines a validation rule that ensures that the Position.Title property has a value when the Position object is saved.
- RuleValuefield 属性定义一个验证规则,以确保在保存"位置"对象时,位置.Title 属性具有值。
- Run the WinForms or ASP.NET 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.
WinForms Application
- 运行 WinForms 或ASP.NET应用程序。单击"新建(button_new)"按钮创建新职位。将"标题"属性留空并单击"保存"按钮。将显示以下错误消息,具体取决于应用程序类型。
WinForms 应用程序

ASP.NET Application
ASP.NET应用程序

This warning message will also be invoked if you click the Save and Close button, or perform another action that saves the object to the database.
如果单击"保存和关闭"按钮,或执行将对象保存到数据库的另一个操作,也会调用此警告消息。
Note
注意
You can use the Validate toolbar button to check to see if there are broken rules without saving the current object.
您可以使用"验证工具栏"按钮来检查是否有损坏的规则,而无需保存当前对象。
- 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 application, set a value for the Title property and click the Save button. The object will be saved successfully.
- 在 WinForms 应用程序中,关闭带有警告消息的窗口,为 Title 属性设置值,然后单击"保存"按钮。在ASP.NET应用程序中,为 Title 属性设置值,然后单击"保存"按钮。对象将成功保存。
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 Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/
您可以在 MySolution.模块 |业务对象 |Contact.cs (Contact.vb) 文件的主演示安装与 XAF.主演示应用程序安装在%PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/
.
Implement Property Value Validation in Code 在代码中实现属性值验证(XPO)的更多相关文章
- XAF-在代码中实现属性值验证(EF)
This lesson explains how to set rules for business classes and their properties. These rules are val ...
- Implement Property Value Validation in the Application Model 在应用程序模型中实现属性值验证
In this lesson, you will learn how to check whether or not a property value satisfies a particular r ...
- <s:property="a" value=""/>取的<s:debug></s:debug>中的value stack中的属性值
<s:property="a" value=""/>取的<s:debug></s:debug>中的value stack中 ...
- Access the Security System in Code 在代码中访问安全系统
This lesson will guide you through using the static SecuritySystem class to check whether or not a u ...
- 【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
什么是 PaaS?Platform as a Service 平台即服务 (PaaS) 是云中的完整开发和部署环境,你可以使用其中资源交付内容,从基于云的简单应用到启用云的复杂企业应用程序皆可.你以即 ...
- How do I duplicate a resource reference in code behind in WPF?如何在WPF后台代码中中复制引用的资源?
原文 https://stackoverflow.com/questions/28240528/how-do-i-duplicate-a-resource-reference-in-code-behi ...
- Code Snippets 代码片段
Code Snippets 代码片段 1.Title : 代码片段的标题 2.Summary : 代码片段的描述文字 3.Platform : 可以使用代码片段的平台,有IOS/OS X/ ...
- Entity Framework 5.0系列之自动生成Code First代码
在前面的文章中我们提到Entity Framework的"Code First"模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Framework P ...
- 自动生成Code First代码
自动生成Code First代码 在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Fram ...
随机推荐
- C# DataTable to List<T> based on reflection.
From https://www.cnblogs.com/zjbky/p/9242140.html static class ExtendClass { public static List<T ...
- CRM、ERP是什么?
CRM 全称 Customer Relationship Management,中文意思是客户关系管理. 为什么会有CRM? 因为我们进入到了产能过剩时代,从卖方市场过渡到买方市场,为了将产品卖出去, ...
- Dynamics 365 Portal Onpremise缓存问题
最近被Dynamics 365 Portal的缓存问题折腾得不轻,Portal的配置进行缓存也就算了,连CRM中的记录也进行了长达15分钟到2小时的缓存,这是完全无法接受的 试想,我们有一个Porta ...
- Hack the De-ICE: S1.120 VM (Boot to Root)
下载地址: https://www.vulnhub.com/entry/de-ice-s1120,10/ 静态IP:192.168.1.120 主机扫描: ╰─ nmap -p1-65535 -sV ...
- CROSS-ENV不同环境配置
项目背景 为了适应h5环境搭建需求,需要动态配置开发,测试,生产三种对应域名及其及打包命令.使用cross-env可以让配置环境更加清晰明了还好管理. 简介 cross-env的作用是不需要全局配置N ...
- CentOS 7上的进程管理
一些杂乱的基础概念 程序是一种静态的文件,躺在磁盘上.而进程则是将程序运行起来放置于内存中.因此进程就是运行中的程序,是程序运行起来的一个实例.同一个程序可以运行为多个进程/实例. 进程之间有父子关系 ...
- ceph工作原理
一.概述 Ceph是一个分布式存储系统,诞生于2004年,最早致力于开发下一代高性能分布式文件系统的项目.随着云计算的发展,ceph乘上了OpenStack的春风,进而成为了开源社区受关注较高的项目之 ...
- Docker安全扫描工具之Anchore
本篇简单介绍一款Docker安全扫描工具Anchore的安装和使用. 前言 下述过程是在CentOS 7.6的虚拟机上进行的. [root@localhost ~]# cat /etc/redhat- ...
- COSCon'19 | 如何设计新一代的图数据库 Nebula
11 月 2 号 - 11 月 3 号,以"大爱无疆,开源无界"为主题的 2019 中国开源年会(COSCon'19)正式启动,大会以开源治理.国际接轨.社区发展和开源项目为切入点 ...
- python高阶函数—filter
python内置了一个filter函数,用于过滤序列.和map函数类似,filter()函数也接受一个函数和一个序列.只不过filter函数中是把函数依次作用于序列中的每一个元素,如果是True则保留 ...