diff -r cb5dfea92285 -r b6e250dd7a7d web/form.py --- a/web/form.py Fri Apr 23 11:10:30 2010 +0200 +++ b/web/form.py Fri Apr 23 12:42:53 2010 +0200 @@ -14,8 +14,7 @@ from cubicweb.appobject import AppObject from cubicweb.view import NOINDEX, NOFOLLOW -from cubicweb.web import httpcache, formfields, controller - +from cubicweb.web import httpcache, formfields, controller, formwidgets as fwdgs class FormViewMixIn(object): """abstract form view mix-in""" @@ -139,8 +138,9 @@ @iclassmethod def field_by_name(cls_or_self, name, role=None): - """return field with the given name and role. - Raise FieldNotFound if the field can't be found. + """Return field with the given name and role. + + Raise :exc:`FieldNotFound` if the field can't be found. """ for field in cls_or_self._fieldsattr(): if field.name == name and field.role == role: @@ -149,31 +149,49 @@ @iclassmethod def fields_by_name(cls_or_self, name, role=None): - """return a list of fields with the given name and role""" + """Return a list of fields with the given name and role.""" return [field for field in cls_or_self._fieldsattr() if field.name == name and field.role == role] @iclassmethod def remove_field(cls_or_self, field): - """remove a field from form class or instance""" + """Remove the given field.""" cls_or_self._fieldsattr().remove(field) @iclassmethod def append_field(cls_or_self, field): - """append a field to form class or instance""" + """Append the given field.""" cls_or_self._fieldsattr().append(field) @iclassmethod - def insert_field_before(cls_or_self, new_field, name, role='subject'): - field = cls_or_self.field_by_name(name, role) + def insert_field_before(cls_or_self, field, name, role=None): + """Insert the given field before the field of given name and role.""" + bfield = cls_or_self.field_by_name(name, role) fields = cls_or_self._fieldsattr() - fields.insert(fields.index(field), new_field) + fields.insert(fields.index(bfield), field) + + @iclassmethod + def insert_field_after(cls_or_self, field, name, role=None): + """Insert the given field after the field of given name and role.""" + afield = cls_or_self.field_by_name(name, role) + fields = cls_or_self._fieldsattr() + fields.insert(fields.index(afield)+1, field) @iclassmethod - def insert_field_after(cls_or_self, new_field, name, role='subject'): - field = cls_or_self.field_by_name(name, role) - fields = cls_or_self._fieldsattr() - fields.insert(fields.index(field)+1, new_field) + def add_hidden(cls_or_self, name, value=None, **kwargs): + """Append an hidden field to the form. `name`, `value` and extra keyword + arguments will be given to the field constructor. The inserted field is + returned. + """ + kwargs.setdefault('ignore_req_params', True) + kwargs.setdefault('widget', fwdgs.HiddenInput) + field = formfields.StringField(name=name, value=value, **kwargs) + if 'id' in kwargs: + # by default, hidden input don't set id attribute. If one is + # explicitly specified, ensure it will be set + field.widget.setdomid = True + cls_or_self.append_field(field) + return field def session_key(self): """return the key that may be used to store / retreive data about a