[Checkins] SVN: z3c.form/trunk/ Handle Invalid exceptions as well as ValidationError
Martin Aspeli
optilude at gmx.net
Fri Feb 26 10:12:49 EST 2010
Log message for revision 109471:
Handle Invalid exceptions as well as ValidationError
Changed:
U z3c.form/trunk/CHANGES.txt
U z3c.form/trunk/src/z3c/form/field.py
U z3c.form/trunk/src/z3c/form/field.txt
-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt 2010-02-26 14:29:07 UTC (rev 109470)
+++ z3c.form/trunk/CHANGES.txt 2010-02-26 15:12:48 UTC (rev 109471)
@@ -5,6 +5,8 @@
2.3.3 (unreleased)
------------------
+- Handle Invalid exceptions from constraints and field validators.
+
- Don't create unnecessary self.items in update() method of
SelectWidget in DISPLAY_MODE. Now items is a property.
Modified: z3c.form/trunk/src/z3c/form/field.py
===================================================================
--- z3c.form/trunk/src/z3c/form/field.py 2010-02-26 14:29:07 UTC (rev 109470)
+++ z3c.form/trunk/src/z3c/form/field.py 2010-02-26 15:12:48 UTC (rev 109471)
@@ -307,7 +307,7 @@
getattr(widget, 'field', None),
widget),
interfaces.IValidator).validate(value)
- except (zope.schema.ValidationError,
+ except (zope.interface.Invalid,
ValueError, MultipleErrors), error:
view = zope.component.getMultiAdapter(
(error, self.request, widget, widget.field,
Modified: z3c.form/trunk/src/z3c/form/field.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/field.txt 2010-02-26 14:29:07 UTC (rev 109470)
+++ z3c.form/trunk/src/z3c/form/field.txt 2010-02-26 15:12:48 UTC (rev 109471)
@@ -330,7 +330,12 @@
>>> class LastNameTooShort(zope.schema.interfaces.ValidationError):
... """The last name is too short."""
-
+
+ >>> def lastNameConstraint(value):
+ ... if value and value == value.lower():
+ ... raise zope.interface.Invalid(u"Name must have at least one capital letter")
+ ... return True
+
>>> class IPerson(zope.interface.Interface):
... id = zope.schema.TextLine(
... title=u'ID',
@@ -342,7 +347,8 @@
... title=u'Last Name',
... description=u"The person's last name.",
... default=u'',
- ... required=True)
+ ... required=True,
+ ... constraint=lastNameConstraint)
...
... firstName = zope.schema.TextLine(
... title=u'First Name',
@@ -707,6 +713,7 @@
>>> from z3c.form import error
>>> zope.component.provideAdapter(error.ErrorViewSnippet)
+ >>> zope.component.provideAdapter(error.InvalidErrorViewSnippet)
Let's now cause a widget-level error by not submitting the required last
name:
@@ -719,6 +726,24 @@
>>> manager.extract()
({'firstName': u'Stephan'}, (<ErrorViewSnippet for RequiredMissing>,))
+Or, we could violate a constraint. This constraint raises Invalid, which is
+a convenient way to raise errors where we mainly care about providing a custom
+error message.
+
+ >>> request = TestRequest(form={
+ ... 'form.widgets.firstName': u'Stephan',
+ ... 'form.widgets.lastName': u'richter',
+ ... 'form.widgets.id': u'srichter'})
+ >>> manager = field.FieldWidgets(personForm, request, context)
+ >>> manager.ignoreContext = True
+ >>> manager.update()
+ >>> extracted = manager.extract()
+ >>> extracted
+ ({'firstName': u'Stephan'}, (<InvalidErrorViewSnippet for Invalid>,))
+
+ >>> extracted[1][0].createMessage()
+ u'Name must have at least one capital letter'
+
Finally, let's ensure that invariant failures are also caught:
>>> request = TestRequest(form={
More information about the checkins
mailing list