Validation question

Aug 12, 2013 at 3:04 PM
Edited Aug 12, 2013 at 3:04 PM

I very much prefer writing my valididation rules in such a way that I have one rule for validation of, lets say, Email addresses.
I write it once.

There will be many ViewModels and Classes using a email a property/input box but they should all follow the same rule.

Is it possible to use your validation framework in such a way?

Aug 12, 2013 at 4:04 PM
Hi Lg,

You have two options and it really depends on if you want to use attributes for validation or not

1) Create your own attribute that inherits from ValidationAttribute

2) Use Fluent validation and write your own FluentValidator that implements the IFluentValidator<T> interface

ruleProvider.AddRule("EmailRule").Validatior(x => x.EmailProperty, new EmailValidator());

Hopefully this meets your needs
Aug 12, 2013 at 5:02 PM

thanks. I will give it a try.

There is another thing.

I have a class:
public class User ( )
  public Email email {...}

Then in my Datamodel I do like :

Public User NewUser {...}

Somewhere in my View I have the following binding:

<TextBox Text={ Binding NewUser.Email.Description Mode=TwoWay View:Validation.Property="Description" }
<TextBox Text={ Binding NewUser.Email.Address Mode=TwoWay View:Validation.Property="Address" }

Will this work (I have omitted some code)
Aug 12, 2013 at 6:48 PM
Hi Lg,

So it can support that use but there is a little more configuration than just that.

I'll see about putting together an example either tonight or tomorrow night (might not have time tonight ... fantasy football draft ;)

Essentially what needs to happen is you need to tell the framework you are interested in validating Email addresses inside of User and validating User when it's inside of your viewmodel.

I'll probably put it all together as a blog for future reference and I'll post a link here.