I have spent the last month(s) working on the new
z3c.formjs package. This package builds on Stephan Richter’s recent release of z3c.form (a new and improved, built from the ground up, uber pluggable component architectured form framework). The primary goal of
Hello World (the old way)
Let’s imagine we want a simple form built with any form framework (formlib if you insist) with just one button “Click Me!”.
I won’t actually write the code for this because it would take too long, but I will explain generally how it is done.
- Write your form view class (using z3c.form or formlib) with one button/action/whateveritscalled defined in the form
- register this file as a resource
- create a viewlet for this resource. Don’t forget the viewlet manager, if you don’t have it already
If it is this much trouble, you might as well put everything into a custom page template, which unfortunately will be a pain to extend and maintain.
Hello World (the z3c.formjs way)
This time I will actually write the code because it is just so short:
class HelloWorldForm(layout.FormLayoutSupport, form.Form): #using z3c.form buttons = button.Buttons(jsbutton.JSButton('clickme', title=u'Click Me!')) @jsaction.handler(buttons['clickme']) def clickMeHandler(self, event, selector): return 'alert("Hello World!");'
As much as I’d like to write more, that’s it.
What has been accomplished
- arbitrary DOM elements
- Validate field values on the fly using ajax
- Create handlers for ajax requests in a form
- Use jQuery without having to write renderers (they are already done)
I won’t go over how each of these things work here because it is all in the various *.txt files located in the package. These *.txt files are also fairly thorough tests at 99% test coverage. In fact you can see recent coverage reports here: http://beta.carduner.net/docs/z3c.form-coveragereport/all.html.
For your convenience and reading pleasure, you can look at the *.txt files below (they are iframes linked to the svn files).
What is next?
There is still plenty left that can be done. And some of those things will get done! They are the following:
- ajax form submission (think of the “save” button in gmail)
- ajax widget switching: switching a display widget for an edit widget
svn co svn://svn.zope.org/repos/main/z3c.formjsdemo/trunk z3c.formjsdemo cd z3c.formjsdemo svn co svn://svn.zope.org/repos/main/z3c.formjs/trunk z3c.formjs svn co svn://svn.zope.org/repos/main/z3c.form/trunk z3c.form python2.4 bootstrap.py ./bin/buildout -Nv ./bin/demo fg
Yes, I know, that is not *quite* easy, but there should be an egg available on cheeseshop within a couple days for a 0.2 release. I’ll announce that on this blog as well.