Skip to content

Commit

Permalink
- Added labelRequired and requiredInfo form attributes. This is
Browse files Browse the repository at this point in the history
  useful for conditional rendering a required info legend in form templates.
  The requiredInfo label depends by default on a given lableRequired message id
  and will only return the label if at least one widget field is required.
- added tests
  • Loading branch information
projekt01 committed Feb 22, 2009
1 parent a5e1dab commit 2c7df07
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Expand Up @@ -5,6 +5,11 @@ CHANGES
Version 2.0.0 (unreleased)
--------------------------

- Feature: Added labelRequired and requiredInfo form attributes. This is
useful for conditional rendering a required info legend in form templates.
The requiredInfo label depends by default a given lableRequired message id
and will only return the label if at least one widget field is required.

- Feature: Add support for refreshing actions after their execution. This
is useful when button action conditions are changing as a result of
action execution. All you need is to set the ``refreshActions`` flag
Expand Down
3 changes: 3 additions & 0 deletions src/z3c/form/field.py
Expand Up @@ -179,6 +179,7 @@ class FieldWidgets(util.Manager):
prefix = 'widgets.'
mode = interfaces.INPUT_MODE
errors = ()
hasRequiredFields = False
ignoreContext = False
ignoreRequest = False
ignoreReadonly = False
Expand Down Expand Up @@ -275,6 +276,8 @@ def update(self):
widget.update()
zope.event.notify(AfterWidgetUpdateEvent(widget))
# Step 9: Add the widget to the manager
if widget.required:
self.hasRequiredFields = True
uniqueOrderedKeys.append(shortName)
if newWidget:
self._data_values.append(widget)
Expand Down
11 changes: 11 additions & 0 deletions src/z3c/form/field.txt
Expand Up @@ -622,6 +622,17 @@ this selection for you:
'input'


Required fields
---------------

There is a flag for required fields. This flag get set if at least one field
is required. This let us render a required info legend in forms if required
fields get used.

>>> manager.hasRequiredFields
True


Data extraction and validation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions src/z3c/form/form.py
Expand Up @@ -106,6 +106,7 @@ class BaseForm(browser.BrowserPage):
fields = field.Fields()

label = None
labelRequired = _('<span class="required">*</span>&ndash; required')
prefix = 'form.'
status = ''
template = None
Expand All @@ -130,6 +131,12 @@ def updateWidgets(self):
self.widgets.ignoreReadonly = self.ignoreReadonly
self.widgets.update()

@property
def requiredInfo(self):
if self.labelRequired is not None and self.widgets is not None \
and self.widgets.hasRequiredFields:
return zope.i18n.translate(self.labelRequired, context=self.request)

def extractData(self, setErrors=True):
'''See interfaces.IForm'''
self.widgets.setErrors = setErrors
Expand Down
13 changes: 13 additions & 0 deletions src/z3c/form/form.txt
Expand Up @@ -604,6 +604,19 @@ methods:
...
</html>

The form also provides a label for rendering a required info. This required
info depends by default on the given requiredInfo label and if at least one
field is required:

>>> addForm.requiredInfo
u'<span class="required">*</span>&ndash; required'

If we set the labelRequired to None, we do not get a requiredInfo label:

>>> addForm.labelRequired = None
>>> addForm.requiredInfo is None
True


Changing Widget Attribute Values
--------------------------------
Expand Down
13 changes: 13 additions & 0 deletions src/z3c/form/interfaces.py
Expand Up @@ -603,6 +603,13 @@ class IWidgets(IManager):
default=False,
required=True)

hasRequiredFields = zope.schema.Bool(
title=_('Has required fields'),
description=_('A flag set when at least one field is marked as '
'required'),
default=False,
required=False)

#ugly thing to remove setErrors parameter from extract
setErrors = zope.schema.Bool(
title=_('Set errors'),
Expand Down Expand Up @@ -871,6 +878,12 @@ class IForm(zope.interface.Interface):
'used in the UI.'),
required=False)

labelRequired = zope.schema.TextLine(
title=_('Label required'),
description=_('A human readable text describing the form that can be '
'used in the UI for rendering a required info legend.'),
required=False)

prefix = zope.schema.BytesLine(
title=_('Prefix'),
description=_('The prefix of the form used to uniquely identify it.'),
Expand Down

0 comments on commit 2c7df07

Please sign in to comment.