When CGI ruled the Web, CGI::FormBuilder was one of the good ways of keeping the form DRY by having the same code generate the form and validate passed parameters once it is submitted.

I am writing a small Dancer2 web application which needs to have a form. Originally I had considered generating and validating the form from the same description, but then I realised that such generated form would be limited in terms of layout I want to present to the user. For example, most of the fields follow a simple template:

<p> <label for="[% id %]">[% name %] = </label> <input name="[% id %]" placeholder="[% placeholder %]" type="text" id="[% id %]" [% required %] pattern="[% regex %]" > <label for="[% id %]">[% units %]</label> </p>
But a few fields are more complicated, for example, there is two fields per one item description:
<p> <label for="lowwl">Wavelength:</label> <input name="lowwl" placeholder="400" type="text" id="lowwl" pattern="(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?" > <label> - </label> <input name="upwl" placeholder="600" type="text" id="upwl" pattern="(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?" > <label for="upwl">nm</label> </p>
What are the best practices for form design in 2019? Should I generate the form? (if so, how should I structure the form description if I have to account for various deviations from the template?) Should I keep the form and the form code separate, but complain loudly in case I receive an unexpected parameter? (my original goal in pursuing DRY is preventing typos and facilitating later updates)


In reply to Form generation and validation by Anonymous Monk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.