[Checkins] SVN: z3c.form/trunk/src/z3c/form/util. Fix getWidgetById method, recognize the id/name changes

Roger Ineichen roger at projekt01.ch
Wed Jun 20 08:10:55 EDT 2007


Log message for revision 76834:
  Fix getWidgetById method, recognize the id/name changes

Changed:
  U   z3c.form/trunk/src/z3c/form/util.py
  U   z3c.form/trunk/src/z3c/form/util.txt

-=-
Modified: z3c.form/trunk/src/z3c/form/util.py
===================================================================
--- z3c.form/trunk/src/z3c/form/util.py	2007-06-20 10:20:31 UTC (rev 76833)
+++ z3c.form/trunk/src/z3c/form/util.py	2007-06-20 12:10:55 UTC (rev 76834)
@@ -72,10 +72,12 @@
 
 def getWidgetById(form, id):
     """Get a widget by it's rendered DOM element id."""
+    # convert the id to a name
+    name = id.replace('-', '.')
     prefix = form.prefix + form.widgets.prefix
-    if not id.startswith(prefix):
-        raise ValueError("Id %r must start with prefix %r" %(id, prefix))
-    shortName = id[len(prefix):]
+    if not name.startswith(prefix):
+        raise ValueError("Name %r must start with prefix %r" %(name, prefix))
+    shortName = name[len(prefix):]
     return form.widgets.get(shortName, None)
 
 

Modified: z3c.form/trunk/src/z3c/form/util.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/util.txt	2007-06-20 10:20:31 UTC (rev 76833)
+++ z3c.form/trunk/src/z3c/form/util.txt	2007-06-20 12:10:55 UTC (rev 76834)
@@ -32,20 +32,21 @@
 
 We can now ask for the widget:
 
-  >>> util.getWidgetById(addPerson, 'form.widgets.name')
+  >>> util.getWidgetById(addPerson, 'form-widgets-name')
   <TextWidget 'form.widgets.name'>
 
 The widget id can be split into a prefix and a widget name. The id must always
 start with the correct prefix, otherwise a value error is raised:
 
-  >>> util.getWidgetById(addPerson, 'myform.widgets.name')
+  >>> util.getWidgetById(addPerson, 'myform-widgets-name')
   Traceback (most recent call last):
   ...
-  ValueError: Id 'myform.widgets.name' must start with prefix 'form.widgets.'
+  ValueError: Name 'myform.widgets.name' must start with prefix 'form.widgets.'
 
 If the widget is not found but the prefix is correct, ``None`` is returned:
 
-  >>> util.getWidgetById(addPerson, 'form.widgets.myname')
+  >>> util.getWidgetById(addPerson, 'form-widgets-myname') is None
+  True
 
 
 extractFileName



More information about the Checkins mailing list