3 ms·
If I'm understanding you correctly, you want to have the fields on the form depend on the contents of what was entered on the previous form e.g. if user selects
by scott_w 14y ago
If I'm understanding you correctly, you want to have the fields on the form depend on the contents of what was entered on the previous form e.g. if user selects name, ask for name, otherwise ask for address.
In that case, I tend to use class-based views and set different forms, templates etc. depending on those values. If the form is different enough, it's easier just to send them to a completely different URL.
- porker 14y agoNot quite - that happens sometimes and I use logic in the controller (ugh) to switch the form class. This is what I mean: an adaptive form http://screencast.com/t/SutggDU5 http://screencast.com/t/SutggDU5 The server-side validation has to adapt depending on the data coming from the client-side, and varies a lot. Perhaps others don't build forms like this much and so web frameworks don't deal with them; I get one like this every few months!
- scott_w 14y agoAh, I see now. For certain cases e.g. repeating the same form over and over, you can use formsets, but that doesn't work when you have two different forms. I still think a solution is to use multiple forms and form types rather than one big form. Perhaps you can prefix the field names on the template and use that to determine which form they go to inside the view. It makes your logic a little simpler in any case.
- masklinn 14y agoNote that one thing you can do is build smaller forms and mix them up. There's no issue with mixing up 3 or 4 smaller (django) forms in the same (HTML) form, quite the opposite.
- TomaszZielinski 13y agoRe server-side validation that adapts to the incoming data, I think it's easier to write a small mixin that fits your needs than to use a generic library (assuming there's one).