Some time ago I wrote this generic AJAX Django form validation code. Some people didn’t like this, as AJAX should not be used to perform form validation, which is sometimes true, sometimes not, as I pointed out before.
The resulting project is called django-validation. It basicly allows one to write a Newforms form class, and generate client-side validation code for this form in a template. Currently only CharField validation is implemented, more should follow soon and is easy to add.
Next to validation of built-in field types, one can also add code to validate custom fields. This can be done inside an inner class of the field class.
The current release is very alpha-grade software, a lot of enhancements could be done, and most certainly more standard field type validators should be written. Next to this, field type inheritance isn’t supported for now (so if your field type A inherits CharField, no CharField validation will be done), this should change soon.
Patches are, obviously, very welcome!
Here’s a sample how to use it. First we define a very basic form:
class TestForm(forms.Form): first_name = forms.CharField(max_length=128) test = TestField(required_value=u'I like django-validation')
This form uses a custom class (just for demonstration purposes). This class only performs client-side validation, no clean() function is provided, although in real field types this should obviously be added. More information can be found in the inline comments:
The current code is available in a git repository. Enjoy!