Commits

Matthew Schinckel committed f92caa9 Merge

Merged in marukku/django-jsonfield/field-validation-error (pull request #7)

JSONFormField 'clean' method now calls its super class

Comments (0)

Files changed (2)

jsonfield/forms.py

         widget. So, if we have an object that isn't a string, then for now
         we will assume that is where it has come from.
         """
+        value = super(JSONFormField, self).clean(value)
+
         if not value:
             return value
+
         if isinstance(value, basestring):
             try:
                 return json.loads(value)

jsonfield/tests/base.py

 #:coding=utf-8:
 from django.test import TestCase as DjangoTestCase
 from django.utils import unittest
+from django.utils.encoding import force_text
 from django import forms
 
 from jsonfield.tests.jsonfield_test_app.models import *
         self.assertEquals(type(formfield), JSONFormField)
         self.assertEquals(type(formfield.widget), JSONWidget)
 
+    def test_formfield_clean_blank(self):
+        field = JSONField("test")
+        formfield = field.formfield()
+        self.assertRaisesMessage(forms.ValidationError, force_text(formfield.error_messages['required']), formfield.clean, value='')
+
+    def test_formfield_clean_none(self):
+        field = JSONField("test")
+        formfield = field.formfield()
+        self.assertRaisesMessage(forms.ValidationError, force_text(formfield.error_messages['required']), formfield.clean, value=None)
+
+    def test_formfield_null_and_blank_clean_blank(self):
+        field = JSONField("test", null=True, blank=True)
+        formfield = field.formfield()
+        self.assertEquals(formfield.clean(value=''), '')
+
+    def test_formfield_null_and_blank_clean_none(self):
+        field = JSONField("test", null=True, blank=True)
+        formfield = field.formfield()
+        self.assertEquals(formfield.clean(value=None), '')
+
+    def test_formfield_blank_clean_blank(self):
+        field = JSONField("test", null=False, blank=True)
+        formfield = field.formfield()
+        self.assertEquals(formfield.clean(value=''), '')
+
+    def test_formfield_blank_clean_none(self):
+        field = JSONField("test", null=False, blank=True)
+        formfield = field.formfield()
+        self.assertEquals(formfield.clean(value=None), '')
+
     def test_default_value(self):
         obj = JSONFieldWithDefaultTestModel.objects.create()
         obj = JSONFieldWithDefaultTestModel.objects.get(id=obj.id)