Most Common Form Validation Task

37 views
Skip to first unread message

iaw4

unread,
Mar 4, 2017, 1:51:38 AM3/4/17
to Mojolicious

I have probably written hundreds of html form scripts and validators in my life.  I wonder whether M offers an obvious further simplification of this so-extremely-common task.  Here is what I mean:

I can embed much form field validation information and attributes into my HTML form itself.  For example, my GET may render

  <input type="text" name="email" value="<%= email %>" id="email" width="40" class="emailcss" placeholder="e.g., tr...@whitehouse.com" autofocus required />

However, because the server cannot trust the browser, the recipient also needs to know these constraints.  For example, my maximum width of 40 was in the html form code, which I need to recheck again with the POST recipient.  Moreover, my recipient also has to check that my form really wanted only the fields that the browser sent me back (and not "EMALL").

As good as M::Validator is, it still seems tedious in many (not all) situations.  My most common need is for the POST recipient to be smart enough to consult the original GET form, understand the HTML form request, and validate that all the param are exactly correct: only fields that were requested are filled, all mandatory fields have been filled out, all constraints have been met, etc.  M has a DOM parser built in, so it can presumably it is unusually well positioned to check the GET form.

This seems like an obvious common need.  Has this been written already?  Given that this would solve 9 out of 10 form needs for me (and perhaps many others), with most "programming" being merely html form design, am I reinventing the wheel here?  As a newbie, my code would be so paradigm-strange that it would surely not qualify for use by others.

If it exists, could someone please point me to it?

advice highly appreciated.

/iaw


PS: None of this obviates the need for M::Validator and/or other lower-level mechanisms (which can still be used).   It would just be an additional sub, capable of referring back to the original form.

sri

unread,
Mar 4, 2017, 6:37:08 AM3/4/17
to Mojolicious
I don't think this exists yet. In fact, i don't think this exists for any framework in any language. :)

--
sebastian

iaw4

unread,
Mar 4, 2017, 11:06:52 AM3/4/17
to Mojolicious

thanks, S.

does it seems like a pretty obvious and useful general feature?  html forms even allow the regex validation with the pattern attribute.

ideally, a basic parser briefly looks at the HTML form before it is drawn, encrypts each form (so that it becomes unalterable) and passes it along.  the recipient decrypts these, parses the forms and checks that all the html form constraints are met.  now, without coding, without repetition, and only with html markup, one of the most common tasks in web interaction is taken care of.

I will likely get to rolling my own when I understand the framework a little better.  for the sender, I need to hook post-template processing and pre-rendering, parse the DOM, encrypt each form and add it as hidden field into each form.  for the recipient, I need a function that decrypts and systematically goes through the tags and attributes in the html.

regards,

/iaw
Reply all
Reply to author
Forward
0 new messages