web/form.py
branchstable
changeset 5367 4176a50c81c9
parent 4779 dce36da37d40
child 5368 d321e4b62a10
equal deleted inserted replaced
5366:5f116a4d8a54 5367:4176a50c81c9
   160     def append_field(cls_or_self, field):
   160     def append_field(cls_or_self, field):
   161         """append a field to form class or instance"""
   161         """append a field to form class or instance"""
   162         cls_or_self._fieldsattr().append(field)
   162         cls_or_self._fieldsattr().append(field)
   163 
   163 
   164     @iclassmethod
   164     @iclassmethod
   165     def insert_field_before(cls_or_self, new_field, name, role='subject'):
   165     def insert_field_before(cls_or_self, field, name, role=None):
   166         field = cls_or_self.field_by_name(name, role)
   166         """Insert the given field before the field of given name and role."""
       
   167         bfield = cls_or_self.field_by_name(name, role)
   167         fields = cls_or_self._fieldsattr()
   168         fields = cls_or_self._fieldsattr()
   168         fields.insert(fields.index(field), new_field)
   169         fields.insert(fields.index(bfield), field)
   169 
   170 
   170     @iclassmethod
   171     @iclassmethod
   171     def insert_field_after(cls_or_self, new_field, name, role='subject'):
   172     def insert_field_after(cls_or_self, field, name, role=None):
   172         field = cls_or_self.field_by_name(name, role)
   173         """Insert the given field after the field of given name and role."""
       
   174         afield = cls_or_self.field_by_name(name, role)
   173         fields = cls_or_self._fieldsattr()
   175         fields = cls_or_self._fieldsattr()
   174         fields.insert(fields.index(field)+1, new_field)
   176         fields.insert(fields.index(afield)+1, field)
       
   177 
       
   178     @iclassmethod
       
   179     def add_hidden(cls_or_self, name, value=None, **kwargs):
       
   180         """Append an hidden field to the form. `name`, `value` and extra keyword
       
   181         arguments will be given to the field constructor. The inserted field is
       
   182         returned.
       
   183         """
       
   184         kwargs.setdefault('ignore_req_params', True)
       
   185         kwargs.setdefault('widget', fwdgs.HiddenInput)
       
   186         field = formfields.StringField(name=name, value=value, **kwargs)
       
   187         if 'id' in kwargs:
       
   188             # by default, hidden input don't set id attribute. If one is
       
   189             # explicitly specified, ensure it will be set
       
   190             field.widget.setdomid = True
       
   191         cls_or_self.append_field(field)
       
   192         return field
   175 
   193 
   176     def session_key(self):
   194     def session_key(self):
   177         """return the key that may be used to store / retreive data about a
   195         """return the key that may be used to store / retreive data about a
   178         previous post which failed because of a validation error
   196         previous post which failed because of a validation error
   179         """
   197         """