|XP is just a number|
repeating fields in CGI formsby swiftone (Curate)
|on Feb 05, 2003 at 19:33 UTC||Need Help??|
swiftone has asked for the
wisdom of the Perl Monks concerning the following question:
This seems such a basic problem, I can't imagine there isn't an elegant solution...OTOH, since I haven't encountered it before now, I could be wrong.
I have a CGI form with the same fields for N sets of data, where N isn't hardcoded. For example, imagine a page where you wanted to enter/update multiple sets of data since the data is simple, such as book titles and authors.
We're entering a good amount of data, so we don't want to limit it to one book/author per page/form, but having N sets of info makes it surprisingly painful to feed through Data::FormValidator and HTML::FillInForm.
I have a solution, I'm just not satisfied with it.
I would just have repeated fields within the form, for example:
but I can't just pass it through Data::FormValidator, because I'm really interested in the data at a sub-form level -- blank fields are acceptable if the entire subform is blank. Likewise, if I pass it through HTML::FillInForm, it will work correctly for filled in values, but will repeat previous values on any "blank" fields.
Here is my disliked solution:
In the code that generates the form, (For me, this is Template, but this holds true for any way of generating the form) I append a counter string to the field names, resulting in something like:
Then I have my code cycle through 1..N (or until $q->param() says a given field wasn't passed) and create chunks of "subform" data, that I pass through Data::FormValidator (with a suitably dynamically altered input profile). If I don't like the results, I can just pass it back to HTML::FillInForm and it gives me the desired output.
This solution technically works, but it's cumbersome, makes my HTML templates messy and not designer-friendly, and in general doesn't reflect the elegance I've come to expect from Perl.
So my question is: Which of the different WTDI is cleaner? Thanks in advance.