如何验证自定义属性? [英] how to validate custom properties?

查看:91
本文介绍了如何验证自定义属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与nhibernate验证程序一起玩耍,并得到了几乎完美的解决方案.

I played around with nhibernate validator and got a nearly perfect solution.

我可以定义要验证的属性,并在预保存时完成.但是在某些情况下,它不起作用.

I can define a property to be validated and it's done on pre-save. But I have some cases where it's not working.

假设我有一个名为person的对象,并通过nhibernate将地址(也是一个对象)映射为person的属性(实际上是地址列表).

Let's assume I have an object called person and via nhibernate I mapped the address(also an object) to be a property of person (in fact it's a list of addresses).

当我保存此人时,我的地址未通过验证.

When I save the person my address is not validated.

用于输入信息的表格由部分表格组成.如果可以将地址验证添加到此人的验证列表中会很好,但这不是必需的.

The form to enter the information is made out of partial forms. Would be nice if the address validation could be added to the person's validation list, but thats not required.

我需要一个通用的解决方案,因此无法通过手动"进行验证,例如如果某人随后验证了地址...验证者以某种方式应该看到该属性后面还有一个对象,我也必须对其进行验证.

I need a general solution, I cannot validate by "hand" e.g. if person then validate address... Somehow the validator should see that there is an object behind the property which I also have to validate.

更新:我正在寻找的是一种验证映射对象的方法(hasmany).

Update: What I am looking for is a way to validate mapped objects (hasmany).

推荐答案

切换到最新版本的nhibernate验证程序后,验证适用于子类和映射的类.与xVal 1.0一起使用时,这是一个非常令人满意的解决方案.

After switching to the newest release of nhibernate validator the validation works for subclasses and mapped classes. Together with xVal 1.0 it's a very satisfying solution.

现在,我可以针对每个属性定义要验证的属性(例如,用于正则表达式,长度等),然后通过xVal在客户端通过nHibernate Validator在服务器端获取消息.实际上,它们共享验证模式和错误消息.

Now I can define on every property against what it should be validated (e.g. for a regex, length etc...) and I get the message on client side via xVal, on server side via nHibernate Validator. In fact they share the validation pattern and the error messages.

对于任何需要简单的验证和消息传递定义的nHibernate-Project,我都会推荐这样做.

I would recommend this for any nHibernate-Project where a simple definition for validation and messaging is required.

这篇关于如何验证自定义属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆