[Zope3-checkins] CVS: Zope3/src/zope/app/form - utility.py:1.7

R. David Murray bitz@bitdance.com
Mon, 27 Jan 2003 22:40:40 -0500


Update of /cvs-repository/Zope3/src/zope/app/form
In directory cvs.zope.org:/tmp/cvs-serv13589/zope/app/form

Modified Files:
	utility.py 
Log Message:
setUpWidgets did not have any unit tests for the code path where the names
argument was passed.  I added some.  There was also what I believe is
a bug in that code path: if a name was specified that existed in the
Interface but was not a Field, it was silently ignored.  I rewrote
setUpWidgets to use schema's getFieldNamesInOrder instead of doing the
introspection itself, and the altered code will now generate an error
in the above case.  What happens is that setUpWidget tries to bind
the thing that isn't a field and an AttributeError is thrown.  Perhaps
setUpWidgets should check names-specified Interface names to make sure
they are Fields and throw a KeyError if they are not.  That seems a
little like lying, though, so I'm leaving it the way it is for now
(the resulting error message is clear enough, as it refers to the
'Method' object, which is obviously not a Field).  I put a comment
in the test to explain why the AttributeError occurs.


=== Zope3/src/zope/app/form/utility.py 1.6 => 1.7 ===
--- Zope3/src/zope/app/form/utility.py:1.6	Mon Jan 27 21:56:43 2003
+++ Zope3/src/zope/app/form/utility.py	Mon Jan 27 22:40:08 2003
@@ -35,7 +35,7 @@
 __metaclass__ = type
 
 from zope.component import getView, getDefaultViewName
-from zope.schema import getFieldNamesInOrder
+from zope.schema import getFieldNamesInOrder, getFieldsInOrder
 from zope.schema.interfaces import ValidationError, IField
 from zope.app.interfaces.form import IWidget
 from zope.app.interfaces.form import WidgetsError, MissingInputError
@@ -95,13 +95,12 @@
     """Set up widgets for the fields defined by a schema
 
     """
+    if not names: fields = getFieldsInOrder(schema)
+    else: fields = [ (name, schema[name]) for name in names ]
+    for (name, field) in fields:
+        setUpWidget(view, name, field, initial.get(name),
+                    prefix=prefix, force=force)
 
-    for name in (names or schema):
-        field = schema[name]
-        if IField.isImplementedBy(field):
-            # OK, we really got a field
-            setUpWidget(view, name, field, initial.get(name),
-                        prefix=prefix, force=force)
 
 def setUpEditWidgets(view, schema, content=None, prefix=None, force=0,
                      names=None):