File tree Expand file tree Collapse file tree 3 files changed +13
-1
lines changed Expand file tree Collapse file tree 3 files changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,10 @@ def clean(self, value):
16
16
# Have to jump through a few hoops to make this reliable
17
17
value = super (JSONFormField , self ).clean (value )
18
18
19
+ # allow an empty value on an optional field
20
+ if value is None :
21
+ return value
22
+
19
23
## Got to get rid of newlines for validation to work
20
24
# Data newlines are escaped so this is safe
21
25
value = value .replace ('\r ' , '' ).replace ('\n ' , '' )
Original file line number Diff line number Diff line change 4
4
5
5
class TestForm (Form ):
6
6
json = JSONFormField ()
7
+
8
+ class OptionalForm (Form ):
9
+ json = JSONFormField (required = False )
Original file line number Diff line number Diff line change 1
1
from test_project .app .models import Test
2
- from test_project .app .forms import TestForm
2
+ from test_project .app .forms import TestForm , OptionalForm
3
3
4
4
from django .test import TestCase
5
5
from django .db .utils import IntegrityError
@@ -107,3 +107,8 @@ def test_formfield(self):
107
107
f1 = TestForm (data )
108
108
self .assertTrue (f1 .is_valid ())
109
109
self .assertEqual (f1 .cleaned_data , data )
110
+ f2 = TestForm ({})
111
+ self .assertFalse (f2 .is_valid ())
112
+ f3 = OptionalForm ({})
113
+ self .assertTrue (f3 .is_valid ())
114
+ self .assertEqual (f3 .cleaned_data , {'json' : None })
You can’t perform that action at this time.
0 commit comments