Commits

Anonymous committed 79a2b02

JSONFormField 'clean' method now calls its super class

Calling the super class clean method it's required because it does some
really important checks like assuring input values are not empty when
model field is not blank.
In case of having a not null and not blank JSONField, empty values
where allowed by the form field and ValidationError was raised in model
field 'to_python' method because it couldn't be parsed to JSON.

Comments (0)

Files changed (2)

         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

         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, formfield.error_messages['required'], formfield.clean, value='')
+
+    def test_formfield_clean_none(self):
+        field = JSONField("test")
+        formfield = field.formfield()
+        self.assertRaisesMessage(forms.ValidationError, 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)