To build on perrin's post, the actual module he refers to is Data::FormValidator. It can be hard to learn if you don't know object oriented programming or understand closures. However, once you learned how to use it I found that it is one of the best Form validation tools around.
| [reply] |
It is Data::FormValidator, but it's more than that. The way re-displaying the form with the data still filled is handled is the thing that made me recommend it as an answer for this question.
| [reply] |
I don't think you'll be getting negative comments for asking a question that you've already put some research effort into.
I believe that what you might want to look into is either CGI or CGI::Application. CGI::Application allows you to sidestep that whole "crazy loop" part, and segment your program neatly based on context. For example, the "user entered bad data," "user entered good data," and "new user" parts can all be in their own compartments of your script. CGI will also help you do some of the boring mundane stuff like grab the response itself.
| [reply] |
There are even more options, such as Catalyst, also available on CPAN.
Really, the pandora's-box that you have just unknowingly opened is huge, with a king's ransom of good options to choose from. Therefore, I strongly encourage you to “blow your own mind.” :-)
What I mean is, set-aside the entire concept that you now have of “what I'm going to have to do to solve this problem whatever-it-is.” Allow yourself to step back from what you did (or had to do) “in the past,” and look carefully for existing high-level solutions that will solve the same problem, but that will do so much more comprehensively and thoroughly than you could ever hope to do in the same amount of time.
It is literally true that, while you're struggling to decide just how to equip your wagon with supplies for “the long journey West,” and how to build the roadways you'll need to travel along the way, someone else might come along and say, “Ummm, dude, why not just get on an airplane?” (“Why not just use an antimatter-transporter” surely won't be too far off.) Bear in mind that your true objective is not “to get there,” but rather, “to be there.”
I don't care what your web-site is supposed to be doing: by now, “a web-site” is not “where no man has gone before.”
| |
| [reply] |
If it's really important to keep the existing data which the user has entered so far, I'd say validation via ajax is going to be much easier than the "crazy loops" you mentioned. So this is really more of a javascript question
than a perl question.
Using prototype.js (or if you know jquery) you basically make a regular form which has its onsubmit() handler set to make the ajax call and return false. So the browser won't attempt to change to another page. (Incidentally and off-topic, but for a good example, code like this is what the Rails function "form_remote_tag" auto-generates).
The perl side will be very minimal--just collect the params, validate them however you want and return some JSON with the rejected params, or with a flag indicating success.
There are many examples of code like this to be found on the prototype mailing list (archived on google groups).
| [reply] |
My mantra as a Web developer, who does lots of form processing, is CGI::Application (and some of the great plugins), HTML::Template, and HTML::FillinForm. Good form processing is the discipline of trying to maintain "state" in a "stateless" medium. Also, good practices include proper handling of errors, especially (and this is something you touched on) returning error messages and at the same time getting the good data they entered restored back to the form after untainting, validation, and repopulating dynamically built select tags. A good understanding of sessions and cookies is helpful, but it's tools like HTML::Template and HTML::FillinForm are invaluable.
If you interested in Ajax, take a look at AHAH, a simpler subset of the more formal Ajax ('look Ma, no XML') and can feel a lot more natural for us coders so accustomed to building strings for output.
—Brad "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
| [reply] |