views:

185

answers:

2

I understand thatthe PropertyProxyValidator integrates with the ASP.NET UI. But, it cannot do client side validation. How would it be any different from throwing in a label in the UI and populating the errors on the server side?

Also, If I am using Validation Application Block, what approaches do you suggest for client side validation if I don't want to duplicate rules on server and client side?

+1  A: 

As you say, using the PropertyProxyValidator is not much different than using an ASP.NET validator (or using a label) on the server side. The main advantage is the integration with the Validation Application Block. (Assuming you see that as an advantage. :) )

If you've already created your rules using VAB then you can use the PropertyProxyValidator to integrate them into ASP.NET. If you have a multi-tier application this would allow you to define validations once (lets say using attributes on entities) and validate in ASP.NET server side. If the validation passes, then a business/service tier could be called where the same validation could then be run. This centralizes the validation to a single location (increased maintainability) and can save calls to the business/service tier (performance benefit).

For client side browser validation using VAB, I don't think there are many good options. David Hayden mentions using AJAX to achieve "client-side like validation". The user experience is enhanced but server calls are not saved so this is not a full solution. Another article, Client side validation using Validation Application blocks, outlines parsing the VAB configuration to create client side validators. That approach also has drawbacks (quite a bit of custom code -- how much work are we saving here?).

Tuzo
A: 

The PropertyProxyValidator doesn't help you with client side validation. I think the main difference with 'throwing in a label in the UI and populating the errors on the server side' is that the PropertyProxyValidator enables you to have the validation errors next to the validated control.

Using the PropertyProxyValidator is a lot of work. Everything has to be hooked up. A nicer solution is to create a simple extension method and register the PropertyProxyValidator in the code behind. This makes everything so much easier. Here is an example:

protected override void OnPreInit(EventArgs e)
{
    this.LastNameTextBox.For<Person>().AddValidator(p => p.LastName);
    base.OnPreInit(e);
}

You can find more information about this approach here.

Of course, it's still server side in that case, but this solution makes it much easier to enable client side validation later on, because the it centralizes the creation of the validators.

Tuzo already referenced this article. It's the only reference about client side validation with VAB I've found on the web.

Steven
@Steven - "PropertyProxyValidator enables you to have the validation errors next to the validated control" - Technically, I can have 1 label per control and achieve the same UI result. "Perhaps it's much nicer to do asynchronous postbacks on the background that do validation " If I stick everything in an UpdatePanel (per David Hayden's suggestion), it results in a better UI, but like you said, it's still server side and UpdatePanel is notorious for huge response sizesI was looking for something similar to what the .NET Validation framework http://www.codeplex.com/ValidationFramework does
I think it is great that Validation Framework has this integration right, but please review the features of VF and check what your requirements are. For instance, it only supports attribute based validation. Perhaps you could look at how VF does this ASP.NET integration internally and build an implementation for VAB with that.
Steven