如何在 Castle.ActiveRecord 中使用 ValidateDecimal 属性?

发布于 2024-07-30 01:45:06 字数 611 浏览 2 评论 0 原文

我有一个继承 ActiveRecordValidationBase 的类,其中包含以下属性:

[Property]
[ValidateDecimal]
public Decimal UnitCost { get; set; }

我还有一个 UnitCostTextBox,它接受所述 UnitCost 的输入。

我想做的是使用 Castle 的验证器执行验证一次。 但是,似乎在将 UnitCodeTextBox.Text 传递给我的对象之前,我需要先将其转换为十进制。

如果我输入错误,就会抛出异常。 因此,这意味着我仍然必须执行正则表达式验证并将字符串转换为十进制类型,然后再将其交给 Castle.ActiveRecord。

这是否意味着由于我已经清理了 UnitCost,所以使用 [ValidateDecimal] 是多余的?

我想知道你们是怎么做到的? 我已经用谷歌搜索了示例,但大多数只处理 [ValidateNonEmpty][ValidateEmail] 无论如何,它们都是字符串,而不是不同的数据类型

I have a class that inherits ActiveRecordValidationBase that contains the following property:

[Property]
[ValidateDecimal]
public Decimal UnitCost { get; set; }

I also have a UnitCostTextBox that accepts input for said UnitCost.

What I would like to do is perform validation once using Castle's validators. However, it seems that before I can pass UnitCodeTextBox.Text to my object, I will need to first convert it to a decimal first.

If I have an erroneous input, an exception will be thrown. So this means I still have to perform regex validations and converting the string to a decimal type before handing it over to Castle.ActiveRecord.

Doesn't this mean it's redundant to have a [ValidateDecimal] since I've already sanitized UnitCost?

I'm wondering how do you guys do it? I have googled for examples, but most of them only handle [ValidateNonEmpty] or [ValidateEmail] which are all strings anyway, not different data types

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2024-08-06 01:45:06

您缺少的是绑定部分(实际上您是手动完成的)。 Castle.Components.Binder 和 Castle.Components.Validator 一起使用来自动将字符串输入(例如 HTML 表单)绑定和验证到强类型对象中。

MonoRail 使用 [DataBind] 属性 和 AR 特定的 [ARDataBind]

在 WebForms 应用程序中,您必须自己实现绑定+验证(你当然可以使用 Castle.Components.Binder 和 Castle.Components.Validator)

What you're missing is the binding part (actually you're doing it manually). Castle.Components.Binder and Castle.Components.Validator are used together to automatically bind and validate string input (e.g. an HTML form) into strongly typed objects.

MonoRail does this with the [DataBind] attribute and the AR-specific [ARDataBind]

In a WebForms application you'll have to implement binding+validation yourself (you can of course use Castle.Components.Binder and Castle.Components.Validator)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文