field_by_name method (on class or instance) tls-sprint
authorsylvain.thenault@logilab.fr
Wed, 11 Mar 2009 14:34:27 +0100
branchtls-sprint
changeset 1058 c27be37daef8
parent 1057 6636c75c4aa9
child 1059 87dc8b1903a2
field_by_name method (on class or instance)
web/form.py
--- a/web/form.py	Wed Mar 11 14:33:26 2009 +0100
+++ b/web/form.py	Wed Mar 11 14:34:27 2009 +0100
@@ -12,6 +12,7 @@
 from simplejson import dumps
 
 from logilab.common.compat import any
+from logilab.common.decorators import iclassmethod
 from logilab.mtconverter import html_escape
 
 from yams.constraints import SizeConstraint, StaticVocabularyConstraint
@@ -772,7 +773,18 @@
                     self.form_add_hidden(param, initial=value)
         self.buttons = buttons or []
         self.context = None
-        
+
+    @iclassmethod
+    def field_by_name(cls_or_self, name):
+        if isinstance(cls_or_self, type):
+            fields = cls_or_self._fields_
+        else:
+            fields = cls_or_self.fields
+        for field in fields:
+            if field.name == name:
+                return field
+        raise Exception('field %s not found' % name)
+    
     @property
     def form_needs_multipart(self):
         return any(field.needs_multipart for field in self.fields)