# HG changeset patch # User sylvain.thenault@logilab.fr # Date 1236778467 -3600 # Node ID c27be37daef8b1a7c9f6cd4276f2d59b98df9dd8 # Parent 6636c75c4aa9b7e9d32c30cd81e4421e47d9ca39 field_by_name method (on class or instance) diff -r 6636c75c4aa9 -r c27be37daef8 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)