我有一个继承 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
发布评论
评论(1)
您缺少的是绑定部分(实际上您是手动完成的)。 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)