[Checkins] SVN: z3c.form/trunk/ - Feature: The validation data wrapper now knows about the context of the

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Jul 27 11:51:22 EDT 2007


Log message for revision 78372:
  - Feature: The validation data wrapper now knows about the context of the
    validation, which provides a hook for invariants to access the environment.
  

Changed:
  U   z3c.form/trunk/CHANGES.txt
  U   z3c.form/trunk/src/z3c/form/interfaces.py
  U   z3c.form/trunk/src/z3c/form/validator.py
  U   z3c.form/trunk/src/z3c/form/validator.txt

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2007-07-27 12:52:17 UTC (rev 78371)
+++ z3c.form/trunk/CHANGES.txt	2007-07-27 15:51:21 UTC (rev 78372)
@@ -5,6 +5,9 @@
 Version 1.6.0 (?/??/2007)
 -------------------------
 
+- Feature: The validation data wrapper now knows about the context of the
+  validation, which provides a hook for invariants to access the environment.
+
 - Feature: The BoolTerms term tokens are now cosntants and stay the same, even
   if the label has changed. The choice for the token is "true" and "false". By
   default it used to be "yes" and "no", so you probably have to change some

Modified: z3c.form/trunk/src/z3c/form/interfaces.py
===================================================================
--- z3c.form/trunk/src/z3c/form/interfaces.py	2007-07-27 12:52:17 UTC (rev 78371)
+++ z3c.form/trunk/src/z3c/form/interfaces.py	2007-07-27 15:51:21 UTC (rev 78372)
@@ -83,6 +83,25 @@
 
 # ----[ Validators ]---------------------------------------------------------
 
+class IData(zope.interface.Interface):
+    """A proxy object for form data.
+
+    The object will make all keys within its data attribute available as
+    attributes. The schema that is represented by the data will be directly
+    provided by instances.
+    """
+
+    def __init__(schema, data, context):
+        """The data proxy is instantiated using the schema it represents, the
+        data fulfilling the schema and the context in which the data is
+        validated.
+        """
+
+    __context__ = zope.schema.Field(
+        title=_('Context'),
+        description=_('The context in which the data is validated.'),
+        required=True)
+
 class IValidator(zope.interface.Interface):
     """A validator for a particular value."""
 

Modified: z3c.form/trunk/src/z3c/form/validator.py
===================================================================
--- z3c.form/trunk/src/z3c/form/validator.py	2007-07-27 12:52:17 UTC (rev 78371)
+++ z3c.form/trunk/src/z3c/form/validator.py	2007-07-27 15:51:21 UTC (rev 78372)
@@ -78,10 +78,13 @@
     """
 
 class Data(object):
+    zope.interface.implements(interfaces.IData)
 
-    def __init__(self, schema, data):
+    def __init__(self, schema, data, context):
         self._Data_data___ = data
         self._Data_schema___ = schema
+        zope.interface.alsoProvides(self, schema)
+        self.__context__ = context
 
     def __getattr__(self, name):
         schema = self._Data_schema___
@@ -122,7 +125,7 @@
 
     def validate(self, data):
         """See interfaces.IManagerValidator"""
-        return self.validateObject(Data(self.schema, data))
+        return self.validateObject(Data(self.schema, data, self.context))
 
     def validateObject(self, object):
         errors = []

Modified: z3c.form/trunk/src/z3c/form/validator.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/validator.txt	2007-07-27 12:52:17 UTC (rev 78371)
+++ z3c.form/trunk/src/z3c/form/validator.txt	2007-07-27 15:51:21 UTC (rev 78372)
@@ -287,7 +287,8 @@
 
 So let's start by creating a data object:
 
-  >>> data = validator.Data(IPerson, {'login': 'srichter', 'other': 1})
+  >>> context = object()
+  >>> data = validator.Data(IPerson, {'login': 'srichter', 'other': 1}, context)
 
 When we try to access a name that is not in the schema, we get an attribute
 error:
@@ -308,8 +309,18 @@
   ...     def compute():
   ...         """Compute something."""
 
-  >>> data = validator.Data(IExtendedPerson, {'compute': 1})
+  >>> data = validator.Data(IExtendedPerson, {'compute': 1}, context)
   >>> data.compute
   Traceback (most recent call last):
   ...
   RuntimeError: ('Data value is not a schema field', 'compute')
+
+Finally, the context is available as attribute directly:
+
+  >>> data.__context__ is context
+  True
+
+It is used by the validators (especially invariant validators) to provide a
+context of validation, for example to look up a vocabulary or access the
+parent of an object. Note that the context will be different between add and
+edit forms.



More information about the Checkins mailing list