[web/formfields] Make CompoundField inherit needs_multipart from its subfields
authorDenis Laxalde <denis.laxalde@logilab.fr>
Tue, 21 Jan 2014 17:14:05 +0100
changeset 9444 6012cf57f48e
parent 9443 6ff9401c37c1
child 9445 65d93a4fd11c
[web/formfields] Make CompoundField inherit needs_multipart from its subfields If there's a FileField within a CompoundField, the latter will not have the enctype multipart set, even though the FileField needs it. CompoundField should inspect its children fields to determine whether it needs multipart. Closes #3406930.
web/formfields.py
web/test/unittest_formfields.py
--- a/web/formfields.py	Wed Jan 15 12:34:41 2014 +0100
+++ b/web/formfields.py	Tue Jan 21 17:14:05 2014 +0100
@@ -1033,6 +1033,10 @@
         # while it has no value, hence generating a false error.
         return list(self.fields)
 
+    @property
+    def needs_multipart(self):
+        return any(f.needs_multipart for f in self.fields)
+
 
 class RelationField(Field):
     """Use this field to edit a relation of an entity.
--- a/web/test/unittest_formfields.py	Wed Jan 15 12:34:41 2014 +0100
+++ b/web/test/unittest_formfields.py	Tue Jan 21 17:14:05 2014 +0100
@@ -25,7 +25,7 @@
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.web.formwidgets import PasswordInput, TextArea, Select, Radio
 from cubicweb.web.formfields import *
-from cubicweb.web.views.forms import EntityFieldsForm
+from cubicweb.web.views.forms import EntityFieldsForm, FieldsForm
 
 from cubes.file.entities import File
 
@@ -160,6 +160,21 @@
         field.render(form, renderer)
 
 
+class CompoundFieldTC(CubicWebTC):
+
+    def test_multipart(self):
+        """Ensures that compound forms have needs_multipart set if their
+        children require it"""
+        class AForm(FieldsForm):
+            comp = CompoundField([IntField(), StringField()])
+        aform = AForm(self.request(), None)
+        self.assertFalse(aform.needs_multipart)
+        class MForm(FieldsForm):
+            comp = CompoundField([IntField(), FileField()])
+        mform = MForm(self.request(), None)
+        self.assertTrue(mform.needs_multipart)
+
+
 class UtilsTC(TestCase):
     def test_vocab_sort(self):
         self.assertEqual(vocab_sort([('Z', 1), ('A', 2),