Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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:
<input name="title"> <input name="author"> <hr> <input name="title"> <input name="author"> ..etc
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:

<input name="title01"> <input name="author01"> <hr> <input name="title02"> <input name="author02"> ...etc
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.


In reply to repeating fields in CGI forms by swiftone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2015-07-04 07:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (58 votes), past polls