Skip to content

Commit

Permalink
Initialize widgets in update step, and allow setting prefix string as…
Browse files Browse the repository at this point in the history
… an argument.
  • Loading branch information
malthe committed Oct 30, 2012
1 parent bd086de commit 219af1c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
13 changes: 12 additions & 1 deletion CHANGES.txt
Expand Up @@ -2,9 +2,20 @@
CHANGES
=======

2.9.1 (unreleased)
2.10.0 (unreleased)
------------------

- Initialize widgets in ``update`` step. The ``updateWidgets`` method
is now responsible only for actually updating the widgets.

This allows updating the common widgets prefix before the individual
widgets are updated, useful for situations where neither a form, nor
a widgets prefix is desired.

In addition, the ``updateWidgets`` method has learned an argument
``prefix`` which allows setting the prefix of the field widgets
adapter.

- Capitalize the messages 'no value' and 'select a value'. This change
has been applied also to the existing translations (where
applicable).
Expand Down
8 changes: 5 additions & 3 deletions src/z3c/form/form.py
Expand Up @@ -121,10 +121,10 @@ def getContent(self):
'''See interfaces.IForm'''
return self.context

def updateWidgets(self):
def updateWidgets(self, prefix=None):
'''See interfaces.IForm'''
self.widgets = zope.component.getMultiAdapter(
(self, self.request, self.getContent()), interfaces.IWidgets)
if prefix is not None:
self.widgets.prefix = prefix
self.widgets.mode = self.mode
self.widgets.ignoreContext = self.ignoreContext
self.widgets.ignoreRequest = self.ignoreRequest
Expand All @@ -145,6 +145,8 @@ def extractData(self, setErrors=True):

def update(self):
'''See interfaces.IForm'''
self.widgets = zope.component.getMultiAdapter(
(self, self.request, self.getContent()), interfaces.IWidgets)
self.updateWidgets()

def render(self):
Expand Down
18 changes: 18 additions & 0 deletions src/z3c/form/form.txt
Expand Up @@ -1225,6 +1225,24 @@ Now, if we change those values before updating the widgets, ...
>>> display.widgets.ignoreReadonly
True

We can also set the widget prefix when we update the widgets:

>>> display.updateWidgets(prefix="person")
>>> display.widgets.prefix
'person'

This will affect the individual widgets' names:

>>> display.widgets['id'].name
'form.person.id'

To use unqualified names, we must clear both the form prefix and the
widgets prefix:

>>> display.prefix = ""
>>> display.updateWidgets(prefix="")
>>> display.widgets['id'].name
'id'

Extending Forms
---------------
Expand Down
13 changes: 4 additions & 9 deletions src/z3c/form/group.py
Expand Up @@ -34,8 +34,6 @@ def __init__(self, context, request, parentForm):

def updateWidgets(self):
'''See interfaces.IForm'''
self.widgets = zope.component.getMultiAdapter(
(self, self.request, self.getContent()), interfaces.IWidgets)
for attrName in ('mode', 'ignoreRequest', 'ignoreContext',
'ignoreReadonly'):
value = getattr(self.parentForm.widgets, attrName)
Expand All @@ -44,7 +42,7 @@ def updateWidgets(self):

def update(self):
'''See interfaces.IForm'''
self.updateWidgets()
super(Group, self).update()
groups = []
for groupClass in self.groups:
# only instantiate the groupClass if it hasn't already
Expand Down Expand Up @@ -121,9 +119,10 @@ def applyChanges(self, data):

return changed

def update(self):
def updateWidgets(self):
'''See interfaces.IForm'''
self.updateWidgets()
super(GroupForm, self).updateWidgets()

groups = []
for groupClass in self.groups:
# only instantiate the groupClass if it hasn't already
Expand All @@ -135,7 +134,3 @@ def update(self):
group.update()
groups.append(group)
self.groups = tuple(groups)
self.updateActions()
self.actions.execute()
if self.refreshActions:
self.updateActions()
5 changes: 4 additions & 1 deletion src/z3c/form/interfaces.py
Expand Up @@ -957,11 +957,14 @@ class IForm(zope.interface.Interface):
def getContent():
'''Return the content to be displayed and/or edited.'''

def updateWidgets():
def updateWidgets(prefix=None):
'''Update the widgets for the form.
This method is commonly called from the ``update()`` method and is
mainly meant to be a hook for subclasses.
Note that you can pass an argument for ``prefix`` to override
the default value of ``"widgets."``.
'''

def extractData(setErrors=True):
Expand Down

0 comments on commit 219af1c

Please sign in to comment.