Trihedralguy has asked for the wisdom of the Perl Monks concerning the following question:

Please forgive me that my lack of experience shows in this node.
In the past when I have done basic web applications with perl I would have a few forms here and there and some content read through a database to be interacted with.
My question is, what is the best way to go about handling forms and validation in an easy 'packed' manner. I have been looking into (I think its called) PERL::AJAX which basically does some validation on the fly with some perl. However the draw back from the example that is given is that once the user hits submit and there is an error, the data is lost and the error is displayed. Is there an easier way to keep the flow going without breaking the users data. The amount of data that is going to be inserted in the form i'm creating is substantial so losing the data is a very bad thing.
Other than that I dont need the fanciness of Ajax on the fly validation (its nice to have) if I can just get the information from the user, if the information is bad, return the page with all the data and highlight or display an error list.
For some reason I cannot think in my head how to do this without doing crazy loops, pre-storing all the data, etc.
Please someone with a little experience shead some light on this?
I realize posting this node will get me a lot of negitive comments suggesting should possibily learn how to program a bit more, but this area is always created the biggest brick wall for me.
Any help you can provide of course will be very thanked! :) PS (I hope I'm posting this in the correct section)
  • Comment on Web Application Development with Form Validation

Replies are listed 'Best First'.
Re: Web Application Development with Form Validation
by perrin (Chancellor) on Feb 04, 2008 at 19:00 UTC

      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.

        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.
Re: Web Application Development with Form Validation
by amarquis (Curate) on Feb 04, 2008 at 18:57 UTC

    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.

Re: Web Application Development with Form Validation
by locked_user sundialsvc4 (Abbot) on Feb 04, 2008 at 19:46 UTC

    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.”

Re: Web Application Development with Form Validation
by zby (Vicar) on Feb 04, 2008 at 21:12 UTC
Re: Web Application Development with Form Validation
by Withigo (Friar) on Feb 05, 2008 at 02:14 UTC
    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).
Re: Web Application Development with Form Validation
by bradcathey (Prior) on Feb 05, 2008 at 03:12 UTC

    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