Back to index

plone3  3.1.7
form.py
Go to the documentation of this file.
00001 from plone.fieldsets.interfaces import IFormFieldsets
00002 from zope.formlib import form
00003 
00004 try:
00005     from Products.Five.formlib.formbase import FormBase, EditForm
00006 except ImportError:
00007     from zope.formlib.form import FormBase, EditForm
00008 
00009 
00010 class FieldsetsEditForm(EditForm):
00011     """An edit form which supports fieldsets."""
00012 
00013     def setUpWidgets(self, ignore_request=False):
00014         # First part is copied from zope.formlib.form.EditForm licensed under
00015         # the ZPL 2.1
00016         self.adapters = {}
00017         # In order to support fieldsets, we need to setup the widgets on all
00018         # the fieldsets as well.
00019         if self.is_fieldsets():
00020             self.widgets = None
00021             for fieldset in self.form_fields.fieldsets:
00022                 fieldset.widgets = form.setUpEditWidgets(
00023                     fieldset, self.prefix, self.context, self.request,
00024                     adapters=self.adapters, ignore_request=ignore_request
00025                     )
00026                 if self.widgets is None:
00027                     self.widgets = fieldset.widgets
00028                 else:
00029                     self.widgets += fieldset.widgets
00030         else:
00031             self.widgets = form.setUpEditWidgets(
00032                 self.form_fields, self.prefix, self.context, self.request,
00033                 adapters=self.adapters, ignore_request=ignore_request
00034                 )
00035 
00036     def is_fieldsets(self):
00037         # We need to be able to test for non-fieldsets in templates.
00038         return IFormFieldsets.providedBy(self.form_fields)
00039 
00040 
00041 class FieldsetsInputForm(FormBase):
00042     """An input form which supports fieldsets."""
00043 
00044     def setUpWidgets(self, ignore_request=False):
00045         # In order to support fieldsets, we need to setup the widgets on all
00046         # the fieldsets as well.
00047         if self.is_fieldsets():
00048             self.widgets = None
00049             for fieldset in self.form_fields.fieldsets:
00050                 fieldset.widgets = form.setUpInputWidgets(
00051                     fieldset, self.prefix, self.context, self.request,
00052                     ignore_request=ignore_request
00053                     )
00054                 if self.widgets is None:
00055                     self.widgets = fieldset.widgets
00056                 else:
00057                     self.widgets += fieldset.widgets
00058         else:
00059             self.widgets = form.setUpInputWidgets(
00060                 self.form_fields, self.prefix, self.context, self.request,
00061                 ignore_request=ignore_request
00062                 )
00063 
00064     def is_fieldsets(self):
00065         # We need to be able to test for non-fieldsets in templates.
00066         return IFormFieldsets.providedBy(self.form_fields)