After some refactoring of the existing event hookup API, Stephan Richter and I implemented on the fly AJAX field validation. Here is how you would add validation to your form:
class ValidatorForm(layout.FormLayoutSupport, jsvalidator.MessageValidator, form.Form): zope.interface.implements(interfaces.IAJAXValidator) @jsaction.handler(zope.schema.interfaces.IField, event=jsevent.CHANGE) def fieldValidator(self, event, selector): return self.ValidationScript(self, selector.widget).render()
So the steps are:
- Make your form subclass the
MessageValidatorclass (or any other class that implements
- Implement the
IAJAXValidatorinterface (the right methods and attributes are provided by
- Register the
CHANGEevent on all fields (or just the fields on which you want to have validation) using the handler decorator.
- Return the rendered
- All Done!
MessageValidator class also provides a validate method, which can be accessed by traversing to the url, htp://host/path/to/your/@@form.html/validate. The validate method grabs the value of the field in question from the request, validates it using the regular form validation techniques, and sends back an error message (if validation failed).
You can check this out by looking at the z3c.formjsdemo package (on svn.zope.org).