XAF-在代码中实现属性值验证(EF)
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 (EF)
- Implement Custom Business Classes and Reference Properties (EF)
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.
- 注意
在继续之前,请花点时间复习以下课程。
- 从业务类库类 (EF) 继承
- 实现自定义业务类和参考属性 (EF)
- 验证功能由验证模块提供。将此模块添加到 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 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.
将"规则所需字段属性"属性应用于职位类的"标题"属性。作为参数,指定用于检查规则的上下文(例如,DefaultContexts.Save)。RuleValuefield 属性定义一个验证规则,以确保在保存"位置"对象时,位置.Title 属性具有值。以下代码演示此属性。
using DevExpress.Persistent.Validation;
//...
public class Position {
//...
[RuleRequiredField(DefaultContexts.Save)]
public String Title { get; set; }
}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 或ASP.NET应用程序。单击"新建(button_new)"按钮创建新职位。将"标题"属性留空并单击"保存"按钮。将显示以下错误消息,具体取决于应用程序类型。
WinForms Application
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 EF Demo (Code First) installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\EFDemoCodeFirst.
您可以在 MySolution.模块 |业务对象 |Contact.cs(Contact.vb)文件与XAF一起安装的EF演示(代码优先)文件。默认情况下,EF 演示(代码优先)应用程序安装在 %PUBLIC%_文档_DevExpress 演示 19.2_组件_eXpressApp 框架_EFDemoCodeFirst 中。
XAF-在代码中实现属性值验证(EF)的更多相关文章
- Implement Property Value Validation in Code 在代码中实现属性值验证(XPO)
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 ...
- 在Asp.Net MVC中实现RequiredIf标签对Model中的属性进行验证
在Asp.Net MVC中可以用继承ValidationAttribute的方式,自定制实现RequiredIf标签对Model中的属性进行验证 具体场景为:某一属性是否允许为null的验证,要根据另 ...
- CompareValues标签对Model中的属性进行验证
在Asp.Net MVC中实现CompareValues标签对Model中的属性进行验证 在Asp.Net MVC中可以用继承ValidationAttribute的方式,自定制实现Model两个 ...
- 将source类中的属性值赋给target类中对应的属性
/** * 对象的属性值拷贝 * <p> * 将source对象中的属性值赋值到target对象中的属性,属性名一样,类型一样 * <p> * example: * <p ...
- <s:property="a" value=""/>取的<s:debug></s:debug>中的value stack中的属性值
<s:property="a" value=""/>取的<s:debug></s:debug>中的value stack中 ...
- Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项
本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...
- 【Python】获取翻页之后的各页面中的属性值。
如何获取翻页之后的页面中的html标签中的属性值? # coding=utf-8 from selenium import webdriver if __name__=="__main__& ...
- ajax取到数据后如何拿到data.data中的属性值
今天遇到的ajax取到数据后如何拿到data.data中的属性值的问题 比如拿到了数据 我要取出data中的name 题外话:当然取名最好别取什么奇怪的xiaobi
随机推荐
- 使用Navicat Premium 比较PostgreSql数据库 dev环境与test环境差异
Navicat Premium 功能很强大,支持不同数据库客户端的连接,并且使用工具可以生成两个库差异的sql脚本,方便dev与test环境表结构同步,具体操作方法如下 单击运行,实现两个库中模式表结 ...
- 三大免费开源的php语言cms系统 用好它们让你一天建好一个网站
php语言只所以在web开发领域占据半壁江山,是因为它有太多的生态,成熟的框架体系,广泛的开源cms系统.建设网站的时候,都想提升开发效率,效率就是成本,如果你用原生php语言开发一个项目,既要设计数 ...
- WebGL-3D地图大俯仰角的雾化处理
腾讯位置服务Javascript API GL版,是基于WebGL技术打造的地图API库,使得浏览器环境下也可实现APP端的应用体验,提供2D/3D模式,运行流畅.当前版本提供地图展示.标记.信息窗口 ...
- 流程控制语句if基本概述
目录 1. 流程控制语句if基本概述 2. 流程控制语句if文件比较 判断文件是否存在,返回方式 使用变量的方法进行判断 请输入你要备份的数据库名称: wordpress 请输入你要备份的数据库密码: ...
- FLUME NG的基本架构
Flume简介 Flume 是一个cloudera提供的 高可用高可靠,分布式的海量日志收集聚合传输系统.原名是 Flume OG (original generation),但随着 FLume 功能 ...
- Python语法速查: 6. 循环与迭代
返回目录 (1)while循环与for循环 while仅能用于普通循环,而for除了可以做循环外,还可以遍历序列.集合.字典.迭代器等. 需要注意的是,在类似:for i in somelist: 的 ...
- 201871010131-张兴盼《面向对象程序设计(java)》第一周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://edu.cnblogs.com/campus/xbsf/ ...
- C++ 堆&栈等的说明
Stack 堆 存在于某作用域内的一块空间.说白了就是函数产生的空间,用于存放函数的变量.返回地址. 在函数体中声明的局部变量,就时存储在Stack中. Heap 栈 由操作系统提供的全局空间.在程序 ...
- 洛谷 P4017 最大食物链计数
洛谷 P4017 最大食物链计数 洛谷传送门 题目背景 你知道食物链吗?Delia生物考试的时候,数食物链条数的题目全都错了,因为她总是重复数了几条或漏掉了几条.于是她来就来求助你,然而你也不会啊!写 ...
- [译]发布ABP v0.19包含Angular UI选项
发布ABP v0.19包含Angular UI选项 ABP v0.19已发布,包含解决的~90个问题和600+次提交. 新功能 Angular UI 终于,ABP有了一个SPA UI选项,使用最新的A ...