Widgets are cleared because after zope processes the editform (with Done or Delete button pressed), the selectedItem in the table becomes None, so the addform is then displayed. In the example, the addform and editform have distinct prefixes, so their fields have distinct names in the request object. In particular, the editform doesnt set any of the addform's fields in the request object, so the addform returns with empty/default field values.

The problem is that widgets of grouped fields get the BaseForm's "form." prefix, so the group's widgets in the edit and add forms have the same key in the request object and thus the editform's grouped widgets are named the same as the addform's widgets, so those widget's editform values show up in the addform. (In contrast, non-grouped widgets have the "add." vs "edit." prefix of their parent form.)


One way to fix this is to give groups the prefix of their parentForm. Groups already pass their prefix along to the widgets they create, so those widgets would no longer be prefixed with BaseForm's "form." prefix. Alternatively, setting "ignoreRequest=True" for the addform (so that the addform ignores the insufficiently-distinctly-named values in the request object) may work sometimes (addforms can often ignore requests) but seems less correct.

Proposed fix: in z3c/form/group.py, class Group, method updateWidgets, anywhere before "self.widgets.update()" (or after self.parentForm is assigned in __init__), add line:
self.prefix = util.expandPrefix(self.parentForm.prefix)
(and of course "from z3c.form import util")

John


John wrote:
In modifying the formdemo/addressbook example to use groups, I found that fields in groups are not cleared to their default values when the Done or Delete button is pressed in the editform, resulting in an addform with values from the previous editform. Fields that are not in groups are cleared (as in the original example, which had no groups); only fields in groups have this problem. How can I fix this? And more generally, how are widgets cleared?

Relevant code is attached.  The editform class is below.

thanks!

-------
class CarEditForm(group.GroupForm,form.EditForm):
 form.extends(group.GroupForm,form.EditForm)
 noChangesMessage = None
 fields = field.Fields(ICar).select('nPassengers')
 groups=(carGroup1,)
 prefix = 'car.edit.'
 @button.buttonAndHandler(u'Delete')
 def handleDelete(self, action):
   # Delete the item from the address book
   item = self.getContent()
   addressbook = item.__parent__
   del addressbook[item.__name__]
   # Reset the selected item
   ISession(self.request)[SESSION_KEY]['selectedItem'] = None
 @button.buttonAndHandler(u'Done')
 def handleDone(self, action):
   # Reset the selected item
   ISession(self.request)[SESSION_KEY]['selectedItem'] = None
-------

------------------------------------------------------------------------

_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users
_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to