views:

111

answers:

1

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

A: 

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)

Mauricio Scheffer