lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Aapo Talvensaari once stated:
> 
> What I'm trying to do is to implement HTML form validation as neatly as
> possible:
> 
> 1. everything (almost) comes in as a string
> 2. I need to support type conversions
> 3. some things like checkboxes don't send anything if not checked (so
> absent values doesn't neccessarily mean that something is wrong with form)
> 4. usually you want to do simple cleanups like trimming etc.
> 5. I need to support user defined functions in filter chains (e.g. check if
> user id is in database and it is valid)
> 
> 
> It's pretty hard to come up with nice design, that is easy to use, and easy
> to understand, and still support some edge cases as well... without too
> much magic happening.

  My own homebrew form parsing stuff (uses CGI) is pretty simple to use.  

	cgi = require "cgi"

will read in any parameters given as part of the URL as well as any POST
parameters (if appropriate) and populate two tables:

	cgi.get
	cgi.post

So something like:

	<form method="post">
	  <input name="foo">
	  <checkbox name="bar">
	  <input name="blah">
	</form>

will get turned into

	cgi.post['foo']
	cgi.post['bar']
	cgi.post['blah']

Also, if someone tries something like:

	http://www.example.com/cgiscript?foo=1&foo=apple&foo=foo

(i.e. same parameter name repeated) then what you get back is:

	cgi.get['foo'][1] == "1"
	cgi.get['foo'][2] == "apple"
	cgi.get['foo'][3] = "foo"

(same for post, by the way).  It is then up to the script to check if the
appropriate fields have been send with appropriate data, etc.  I'm not sure
what else could be done to make it easier to use.

  -spc (it supports both "application/x-www-form-urlencoded" (old school)
	and "multipart/form-data" (new school) formats)