Why should I use PropertyProxyValidator? ASP.NET

1.1k Views Asked by At

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?

2

There are 2 best solutions below

2
On BEST ANSWER

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.

0
On

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?).