Ensuring at least min_length subwidgets are rendered for the MultiWidget

Laurence Rowe <[email protected]>
Newsgroups gmane.comp.web.zope.devel
Message-ID <[email protected]>
The attached patch ensures that when in input mode, the MultiWidget
will render at least field.min_length subwidgets. This streamlines the
add process, currently a user only finds out they need to add to a
form when it is submitted and the error message is rendered. They must
then make another request to actually get the add subform.

I've not contributed much to z3c.form yet, so I wanted to check this
approach looked reasonable before committing the patch.

Laurence

_______________________________________________
Zope-Dev maillist  -  [email protected]
https://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 https://mail.zope.org/mailman/listinfo/zope-announce
 https://mail.zope.org/mailman/listinfo/zope )
minwidgets.patch (application/octet-stream, 2.5 KB)
Index: CHANGES.txt
===================================================================
--- CHANGES.txt	(revision 121429)
+++ CHANGES.txt	(working copy)
@@ -5,6 +5,9 @@
 2.4.3 (unreleased)
 ------------------
 
+- Ensure at least min_length widgets are rendered for a MultiWidget in input
+  mode.
+
 - Added base of Czech translation.
 
 - Added Portuguese Brazilian translation.
Index: src/z3c/form/widget.txt
===================================================================
--- src/z3c/form/widget.txt	(revision 121431)
+++ src/z3c/form/widget.txt	(working copy)
@@ -642,6 +642,12 @@
   >>> request = TestRequest()
   >>> multiWidget = widget.FieldWidget(multiField, widget.MultiWidget(request))
 
+Lets ensure that the minimum number of widgets are created.
+
+  >>> multiWidget.update()
+  >>> len(multiWidget.widgets)
+  2
+
 Now, let's check if the function will do the right thing depending on
 the value:
 
Index: src/z3c/form/widget.py
===================================================================
--- src/z3c/form/widget.py	(revision 121429)
+++ src/z3c/form/widget.py	(working copy)
@@ -248,6 +248,7 @@
 
     widgets = None
     _value = None
+    _widgets_updated = False
 
     _mode = FieldProperty(interfaces.IWidget['mode'])
 
@@ -344,6 +345,11 @@
         """Setup internal widgets based on the value_type for each value item.
         """
         oldLen = len(self.widgets)
+        # Ensure at least min_length widgets are shown
+        if (zope.schema.interfaces.IMinMaxLen.providedBy(self.field) and
+            self.mode == interfaces.INPUT_MODE and self.allowAdding and
+            oldLen < self.field.min_length):
+            oldLen = self.field.min_length
         self.widgets = []
         idx = 0
         if self.value:
@@ -359,6 +365,7 @@
                 widget = self.getWidget(idx)
                 self.widgets.append(widget)
                 idx += 1
+        self._widgets_updated = True
 
     def updateAllowAddRemove(self):
         """Update the allowAdding/allowRemoving attributes
@@ -383,6 +390,13 @@
             self.updateWidgets()
         return property(get, set)
 
+    def update(self):
+        """See z3c.form.interfaces.IWidget."""
+        # Ensure that updateWidgets is called.
+        super(MultiWidget, self).update()
+        if not self._widgets_updated:
+            self.updateWidgets()
+
     def extract(self, default=interfaces.NO_VALUE):
         # This method is responsible to get the widgets value based on the
         # request and nothing else.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.