Search code examples
asp.netvalidationenterprise-libraryvalidation-application-bl

Why should I use PropertyProxyValidator? ASP.NET


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?


Solution

  • 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.