step by step problem with validation on "back" button - asp.net-mvc-2

I have a 3 steps forms where I bind for each step on a ViewModel that has DataAnnotation. Everything works fine except that when I click on my "previous" button, the validation gets called and, if there's missing values, the user has to fill all required fields. If I go with an action link, then the data won't be persisted. What I'm looking for here is a way to save my values without calling the validation. I also consider using cookies but I don't think that it's the best way of doing it.
Any ideas or suggestions?

I use separate models for each step of my wizard pages. I also make sure that the previous clicks do not hit ModelState.IsValid which is what triggers the validation check.
I store the results of each step using session state stored in SQL Server. You can also use hidden variables but I didn't like that solution.
Add a comment if you need more detail or show some code for us to see.

Ok after lots of searching I found out a solution for having a Wizard like multiple forms with previous and next buttons that won't call the client validation from Data Annotation and MicrosoftMvcValidation.js. I found this post : jQuery Validation plugin: disable validation for specified submit buttons witch simply add a class to the submit button but it was not working for me so I look again and found this one : http://forums.asp.net/p/1622628/4165648.aspx witch has a solution working for me in javascript : document.getElementById("someButton").disableValidation = true; I would prefer a jQuery solution so I could do something with the class attribute of my buttons but it's working for today and I've spent too mutch time on this.
Hope it helps some else who's trying to do a "Cancel" button or a Wizard like forms in MVC2 and want's to post the form to the controller so he needs to clear the validation.

Related

Zend form application

Im writing a program in zend using zend forms. Everything is fine the form process well to the database but the problem is that the form is too long +-50 inputs which all belong to one database table. How can I find a way to shorten/break some parts of the form to fill in first and then use a next link to fill the rest, bit by bit, and then a finish link to process the form to the database. Basically break the form in parts so that the user wont give up easily or become lazy while filling in his/her details. Can I use multipage form if so, how can I go about it?
Thanks in advance
Use Sub forms for each step (page). See this example - http://framework.zend.com/manual/1.12/en/zend.form.advanced.html#zend.form.advanced.multiPage
As each step will probably contain its own validation, I suppose that the best approach would be to write a separate form for each step in the wizard. After successful validation of one form, store the results in the session and proceed to the next step.

Simulate Form Submit with Symfony 2

I'm working with Symfony 2 and need some advice.
I have a Controller, which gets a form-request and searches the database for matches and renders the results (so it's just a basic search).
Now I would like to redirect to this controller but without coming from a form.
To be more specific:
I'm on the search-page, fill out the form and hit the search button -> I get results for my search.
I'm somewhere else and want to delete a labelCategory of a book -> If some books still use this labelCategory, I want to get search results for this labelCategory.
My only idea so far is, to simulate the form submit somehow, but I didn't find out how to do it.
I'd be happy for every help of you ;)
You don't need form submit at all. Just put logic of searching to separate class. Than you can it in any controller you want. Maybe Service Container doc can help to

Using multiple forms or multiple submit buttons, what's the difference?

Basically, what pros/cons are there to using multiple forms in the same web page vs one form with multiple submit buttons? Any difference at?
Ah? Multiple submit buttons on a single form will all submit the entire form when pressed... there's really no advantage to having multiples, unless you're overriding how the submit process works so each button only submits it's own area. In this case they'd probably not even by submit buttons, but just buttons with sum JS code to handle submission.
Multiple forms are discrete spaces of data collection, each can have it's own submit button... but only one of them can be sent at a time (and depending on the browser you may loose what's in the other forms).
Neither approach is particularly good from a user interface perspective since it'll be confusing.
The real question is, what are you trying to do that prompts you to ask this?
The two behave differently and there are good reasons to choose one over the other.
Multiple Forms on a page allow you to send data to two different locations. A common example is to have an input form as the main focus of a page going to one location, and a search form that appears as part of the generic header/footer. These both go to separate locations and submit only the HTML form elements within their appropriate <form/>
Multiple submit buttons offer you the ability to give different purpose to a submitted set of form elements. E.g. One form may have a bunch of submit buttons all with name attributes, meaning you can add conditional logic on the server side to say: "Continue", go " Back" or even "Save for later". All reference only the form elements within it's parent tag.
Two side notes are: 1) You can't nest forms. 2) JavaScript can change this default behaviour if you wanted it to. :)
Edit: with reference to a comment you made, if you wanted to do without JavaScript (a wise choice while it's not needed), you could do some careful thinking and keep POSTing the form to itself. Each time checking which form button has been clicked (top tip, give them all the same name and you can just switch case through it) and do whatever you need to do, including performing validation. E.g: When they hit "add media", you'd save the media uploaded and return a reference of it to the screen as a hidden input. The user can then continue to add things to the other boxes and when complete, hit your save button, at which point you do all the main saving work and make sure you tie the uploaded file to it as well.
You effectively keep adding stuff to their screen until they hit the save and then you perform a save method and redirect to a thank you page (or whatever logic suits your scenario). :)
All fields in a form are sent when one of their submit button is clicked. It's for you to see if you need all fields or not.

Pattern for Spring-MVC stateful interaction

Today I was doing this thing with Spring:
Have a page with a form and a chance to choose one item related to the form.
If you push "Choose item" the app will save somehow what you typed in the form, go to another page, let you choose the thing.
When you are back to the form it's filled with what you wrote before going to the other page, plus the item chosen.
Seems easy, but you have to take into account that for some stupid reason the user could open the page where you choose the item (maybe because of a bookmark, or because he pressed the back button 10 times to play). You know what I mean. I tried many ways, mainly based on HttpSession... I don't like any of those. None of them seems elegant. I was even thinking of using a hidden form in the other page, but given that it is not unique to this "flow" (I mean you can go to the item choose page from others as well), I will have to worry about conflicts and so on.
So what would be the preferred way for you? Suggestions?
Go around the problem instead of solving it. You can use a modal javascript div popup where the user can pick the item she wants. The contents of this div can be loaded via ajax (separate Spring MVC controller called with Http GET). Once the selection has been made, you close the popup (hide the div) and copy the value into the original form. Done.
No need to store the state anywhere.
I suggest Spring Web Flow.
Spring Web Flow compliments the Spring MVC.
Here is link to Spring Web Flow Demo

MVC2 Multi Select drop down list

I have 2 multi-select Html.DropDownList controls with a button between them. When the user selects items from the first and presses the button they are copied to the second Html.DropDownList control via JQuery.
My problem is when the form is posted back I do not know how to obtain all the values in the second Html.DropDownList control.
This would be really easy with web forms but I have no idea how to do it with MVC2
thanks
--- Also I will need to validate the second Html.DropDownList to ensure it contains at least one item.
Okay, I can get it to work by selecting all the items in the list with JavaScript right before the post back. This feels like such a hack to me though. Surely there is a better way to do it then this. Does anyone know of a way that does not rely on JavaScript?
If relying on JavaScript is the standard way to do this sort of thing then can someone explain why this is okay... Or is this an example of the immaturity of the MVC model?
Thanks.