[Checkins] SVN: z3c.form/trunk/ Add ``z3c.form.error.ComputedErrorViewMessage`` factory for easy creation of dynamically computed error messages.
Dan Korostelev
nadako at gmail.com
Fri Sep 11 06:07:13 EDT 2009
Log message for revision 103731:
Add ``z3c.form.error.ComputedErrorViewMessage`` factory for easy creation of dynamically computed error messages.
Changed:
U z3c.form/trunk/CHANGES.txt
U z3c.form/trunk/src/z3c/form/error.py
U z3c.form/trunk/src/z3c/form/error.txt
-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt 2009-09-11 10:03:01 UTC (rev 103730)
+++ z3c.form/trunk/CHANGES.txt 2009-09-11 10:07:12 UTC (rev 103731)
@@ -5,6 +5,9 @@
Version 2.2.0 (unreleased)
--------------------------
+- Feature: Add ``z3c.form.error.ComputedErrorViewMessage`` factory for easy
+ creation of dynamically computed error messages.
+
- Bug: Replace dots with hyphens when generating form id from its name.
- Refactored OutputChecker to its own module to allow using
Modified: z3c.form/trunk/src/z3c/form/error.py
===================================================================
--- z3c.form/trunk/src/z3c/form/error.py 2009-09-11 10:03:01 UTC (rev 103730)
+++ z3c.form/trunk/src/z3c/form/error.py 2009-09-11 10:07:12 UTC (rev 103731)
@@ -31,7 +31,11 @@
discriminators = ('error', 'request', 'widget', 'field', 'form', 'content')
)
+ComputedErrorViewMessage = value.ComputedValueCreator(
+ discriminators = ('error', 'request', 'widget', 'field', 'form', 'content')
+ )
+
def ErrorViewDiscriminators(
errorView,
error=None, request=None, widget=None, field=None, form=None, content=None):
Modified: z3c.form/trunk/src/z3c/form/error.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/error.txt 2009-09-11 10:03:01 UTC (rev 103730)
+++ z3c.form/trunk/src/z3c/form/error.txt 2009-09-11 10:07:12 UTC (rev 103731)
@@ -91,7 +91,37 @@
Much better, isn't it?
+We can also provide dynamic error view messages that are computed each time we
+get an error. For example, we have an IAdult interface that have minimal age of
+18:
+ >>> class IAdult(zope.interface.Interface):
+ ... age = zope.schema.Int(title=u'Age', min=18)
+
+Now, let's create a function that will be called by a message value adapter,
+it receives one argument, a special ``z3c.form.value.ComputedValue`` object
+that will have all needed attributes: error, request, widget, field, form and
+content. Let's use one of them:
+
+ >>> def getAgeTooSmallErrorMessage(value):
+ ... return u'Given age is smaller than %d, you are not adult.' % (
+ ... value.field.min)
+
+Now, register the computed view message:
+
+ >>> ComputedAgeMessage = error.ComputedErrorViewMessage(
+ ... getAgeTooSmallErrorMessage, error=TooSmall, field=IAdult['age'])
+ >>> zope.component.provideAdapter(ComputedAgeMessage, name='message')
+
+Now, the error view snippet will show dynamically generated message:
+
+ >>> view = error.ErrorViewSnippet(
+ ... TooSmall(), TestRequest(), None, IAdult['age'], None, None)
+ >>> view.update()
+ >>> print view.render()
+ <div class="error">Given age is smaller than 18, you are not adult.</div>
+
+
Registering Custom Error Views
------------------------------
More information about the checkins
mailing list