0

I'm a long time Windows Form developer, trying to get my mind around how to do web development. As the title says, I'm pretty sure I want to move forward with ASP.NET MVC. I'm also pretty sure the answer is going to lie somewhere in the realm of Javascript, jQuery (and jQueryUI), none of which I've had a chance to learn yet.

In our current windows app, we have forms that use tab controls, where each tab control has tons of controls for data entry. So, below are the types of issues I need to be able to do (at the client side)

  1. conditionally require for example that one or more of a subset of controls on a form be required. So for example, because a certain value is selected in a dropdown box, trigger the above rule where one or more controls must be entered... or 1 and only 1 of a subset be conditionally required. Additionally, the subset of conditional required controls can be of different types.

I could go on and on about this but i doubt additional examples will change the answer.

So for clarity, I don't need something like what a think I can do with mvc data annotations, where I can use an attribute to mark a specific field as required, or within a certain range. I need that type of validation buried inside a bunch of other validation.

So, is there a best practice solution for this sort of complex client side validation? Is there some 3rd party Javascript library that might allow this?

In my Windows app, I've been able to write little wrapper rules that all derive from a common base class. So for example, i have a rule with a method to addChilren for example, that all allows you to add either other objects derived from the base class rule, or just other controls, and this rule can be updated at run time to require 1 or more of the children objects...

And then of course this rule "RequireOneOrMore" can then be reused in other places and even be passed as a child to other Rules...

I hope some of this makes sense....

So, while I work through my mvc book towards my Javascript book can anyone suggest what options I might research to accomplish this?

Thanks, jonathan

1 Answer 1

2

I completely agree with you that Data Annotations are very limited in the aspect of performing complex validation rules.

But before even thinking about client side validation you should ensure that all validation rules, no matter how complex, are at least enforced on the server. I great framework for doing complex server side validations that I would recommend you is FluentValidation.NET. It has a great integration with ASP.NET MVC allowing you to handle arbitrarily complex validation rules.

Once you have setup server side validation you could start thinking about the user experience and optionally provide client side validation as well. For complex rules I would recommend you to get rid of the jquery.unobtrusive.validate.js script and then use the jquery.validate.js plugin directly. This means that basically you could write the validation rules yourself for a given form:

$('form').validate({
    rules: {
        // ... here you could write your validation rules
        // you could also extend the plugin by writing custom
        // validation methods to handle arbitrarily complex scenarios
    }
});

I invite you to read the documentation of the plugin now to familiarize yourself with the syntax.

But since you said that you are very new to jQuery, before doing any client side development with validation and plugins, your first task will be to learn jQuery. A great place to start is here: http://learn.jquery.com/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.