Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm working on a small educational project on MVC 3, and I'm facing a small problem, when attempting to handle validation in my application through a wizard.

I tried to get benefit from the ability of MVC3 to deliver content of a different view using the same URL, when handling an [HttpPost] method on a page.

I my case,my main model's class contains about ten [Required] properties, that I would like to expose through a small wizard in 3 steps , So I want that the user may be able to enter his personal informations in the first step, then respond to some questions in the second stepp and finally receive a confirmation mail from the web application whit his credentials in the last step.

I can't access the last step, because of the ModelState.isValid method that I use to handle validations, and which can't perform properly if I define some properties as [Required], but don't put them on the first view.

As the replies to those questions remain in a couple of choices, I've thinked that I may use some nullable bool? for in order to avoid validation issues, but know that it's not the proper way.

Are there someone who would like to help me find a way to extend my validation to those three steps ? Thanks in advance and sorry for my english, I'm not a native speaker.

share|improve this question
add comment

2 Answers

Make a model for each page of the wizard rather than using the domain models you are probably using. Then you can separate the validation as appropriate.

share|improve this answer
    
+1 Exactly. Always use View Models rather than Domain Models, however temping it may be! –  jkoreska Nov 30 '11 at 21:34
add comment

As Wyatt Barnett wrote, the most immediate solution that comes to mind is to have a specific object (one class) for each page of the wizard. If you ware using the Required annotations to make your ModelState.IsValid work, I don't think you can do it another way.

However, I think there is another way to do it as I remember I've done something very similar in Java/Struts some time ago. You could create a new class inheriting from your domain model class and add to that class just one additional property, the current step in the wizard. Now, you can't do your validation using annotations, that's the trade-off. You will have to write your own code for validating your new model (I think you have to implement the IValidatable interface) but you will be able to perform partial validations that depend on the current step in the wizard. Anyway, regardless of the chosen solution (multiple classes or one inheriting class), I think you can't use your existing annotations. The main advantage of this solution is that your wizard matches one class, so whether your wizard spans 10 different pages, or just two, the model to validate does not change, only the validation code.

Maybe you can even write some generic validation rules that will link a property to a step, and then use reflection to validate only properties of that particular step.

Also, remember that you can't really trust a "currentStep" property that comes from the client!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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