1 from django import forms
2 from django.core.exceptions import ValidationError
3 from django.utils import simplejson as json
5 from philo.validators import json_validator
8 __all__ = ('JSONFormField',)
11 class JSONFormField(forms.Field):
12 """A form field which is validated by :func:`philo.validators.json_validator`."""
13 default_validators = [json_validator]
15 def clean(self, value):
16 if value == '' and not self.required:
19 return json.loads(value)
21 raise ValidationError(u'JSON decode error: %s' % e)