[mod_python] vampire form validation

Johannes Erdfelt johannes at erdfelt.com
Tue Oct 19 17:11:25 EDT 2004


As a follow up to my previous patch to make query variable a bit easier
to use in vampire, I have a further reaching suggestion I wanted to
offer up.

In some of my applications, I have some fairly complicated forms that are
in general are a pain to validate that the client sent the correct
variables. When I say validation, I mean at a minimum, requiring the
client send certain variables, and are of the correct base type (integer,
string, etc)

I've written a validator for another project that I'm porting over to
vampire right now. Would there be any interest for a feature like this?

The idea would be that you would specify a new module variable, similar
to the __auth__ variable, called __form_vars__. It would describe the
variables, which are required, what type they should be and possibly a
default value.

The list is built using objects, allowing for extensibility, and is
passed to the application as a class object with the members
automatically created from the name of the variable.

Something like this (I'm in the middle of porting and making some
changes to existing code, so this is a rough draft):

from vampire import form

__form_vars__ = [
  form.integer("userid", required = 0),
  form.submit("submit", [
    form.string("name", required = 1),	# 1 could be the default
    form.string("address"),
    form.string("city"),
    form.string("state"),
    form.string("country", default = "US"),
  ]),
]

The handler would then expect a variable called 'form', which could be
used like this:

  req.write("you requested we create a user with this address:\n")
  req.write("  %s\n" % form.name)
  req.write("  %s\n" % form.address)
  req.write("  %s, %s\n" % (form.city, form.state))
  req.write("  %s\n" % form.state)

Perhaps a form object could be created for HTML radio buttons, allowing
finer grained control over what variables are required:

__form_vars__ = [
  form.radiobutton("choice", {
    "modify": [
      form.string("userid"),
    ],
    "create": [
      form.string("name"),
      form.string("address"),
      ...
    ],
  }),
]

  if form.choice == "modify":
    # modify user
  else:
    # we know this has to be "create" because of the form validation
    # create user

JE



More information about the Mod_python mailing list