Question

I have an Edit view that I use for creating and editing Users. The view has Password1 and Password2 fields, for password and confirmation respectively. These are not marked as required in the view model, as when editing a user, these fields are optional, only used to change a password.

I would not like to have two almost exactly the same view models, differing only by two Required attributes. I do have server validation for these fields, but I have a real dislike for the two-phase validation, where a user fixes all client validation errors, only to submit and wait, and then be presented with server validation errors.

Is there some way I can switch client validation on for these fields in certain cases? I know I can use jQuery to add the necessary attributes, but I was hoping for a simpler and cleaner solution.

Was it helpful?

Solution

I used MVC Foolproof Validation. It has a lovely set of conditional attributes, including [RequiredIf].

OTHER TIPS

I would check out jquery unobtrusive validation as asked about in this other SO question.

The server-side validation can be done via ajax call for more complex validation logic.

Otherwise it seems like a custom rule would be the route to take.

See jQuery Validation documentation for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top