Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanest design pattern for displaying form data and validation errors?

In real-life applications, if I use a form framework, I inevitably end up fighting it at some point.

I'm interested in best practice patterns for building forms in a website. Reference to implementations in any language would be a bonus (I usually work in Python). I'm particularly interested in the problem of creating / displaying the forms to the end user.

Here's how I see the subject. Most parts of the form workflow can be handled with relatively lightweight components that I can pull out of frameworks like formencode and formish. By "most parts" I mean decoding submitted form data into a sensible data structure (e.g. repeated fields in the form should become lists in Python); marshalling the values into the types that we want (e.g. an "age" field should become an integer); and validating the values (e.g. "age must not be blank").

The bit I end up fighting is making and displaying the forms themselves. Many frameworks (e.g. FormAlchemy) tightly couple their schemas, validation and models to some kind of form widget generation scheme, which I don't like.

The tasks involved in making a form are:

  • Making dynamic widgets (e.g. a select list with values from a database)
  • Looping over repeating fieldsets (e.g. a list of name, age pairs that each represent a person)
  • Embedding existing values in the form fields (from a data structure)
  • Displaying validation errors next to form fields or at the top of the page

The options I see for doing this are:

  • generate, based on widgets that are defined in the schema (e.g. Django)
  • post-process forms, by applying a data structure containing errors and values to an HTML template (e.g. htmlfill)
  • accept that for all but the simplest cases, you may as well start from the beginning by encoding the logic (loops, error display, etc) manually in each of your form templates

Are there any other patterns for handling the problem? What are the pros and cons of each?

The frameworks (Python) I've looked at / had pointed out so far are: flatland, formencode, FormAlchemy, formish, WTForms, Django forms, web2py, deform, FormConvert and web.py

Update: I've not really got very far with answering the question about best practise for building forms, but I've made the decision with my current project to go for deform / colander, which is a sane way of handling serialisation etc, but more importantly is well-documented and well-tested. I am going to avoid form generation at all for all but the simplest forms.

like image 674
seb Avatar asked Jan 27 '11 13:01

seb


4 Answers

You can try django. It has forms module, that provides many useful mechanisms for web forms.

like image 100
gruszczy Avatar answered Nov 03 '22 18:11

gruszczy


There's also web.py's Form class which you may find interesting. There are examples in its cookbook.

like image 40
Ivan Avatar answered Nov 03 '22 19:11

Ivan


You might check out web2py, which provides several mechanisms for generating and processing forms (see also form-related Javascript and Ajax functionality). Its form objects (and other HTML helper objects) are server-side representations of the HTML DOM and can be manipulated prior to serialization into HTML (see ref1, ref2, ref3, ref4). If you have further questions about it, ask on the mailing list.

like image 1
Anthony Avatar answered Nov 03 '22 18:11

Anthony


I have recently used flatland to handle a complex form and found it to be very flexible. There's no widgets library, but it has a powerful HTML generator/filter, and as pointed out in the documentation, it's quite simple to write your own widgets. Don't let the "0.0.2" version fool you, the library is quite mature and stable.

like image 1
Alex Morega Avatar answered Nov 03 '22 19:11

Alex Morega