[Checkins] SVN: z3c.form/branches/gocept-invariants/src/z3c/form/ When a value is missing in the form z3c.form.validator.Data no longer raises NoInputData (which causes that an error message is swallowed and interface invariants do not work when not all fields required to compute the invariant are in the form) but looks up missing values on the underlying object if it exists and ignoreContext is not set.

Michael Howitz mh+zope at gocept.com
Thu Sep 27 04:28:43 EDT 2007


Log message for revision 80193:
  When a value is missing in the form z3c.form.validator.Data no longer raises NoInputData (which causes that an error message is swallowed and interface invariants do not work when not all fields required to compute the invariant are in the form) but looks up missing values on the underlying object if it exists and ignoreContext is not set.
  

Changed:
  U   z3c.form/branches/gocept-invariants/src/z3c/form/field.py
  U   z3c.form/branches/gocept-invariants/src/z3c/form/validator.py
  U   z3c.form/branches/gocept-invariants/src/z3c/form/validator.txt

-=-
Modified: z3c.form/branches/gocept-invariants/src/z3c/form/field.py
===================================================================
--- z3c.form/branches/gocept-invariants/src/z3c/form/field.py	2007-09-27 08:24:25 UTC (rev 80192)
+++ z3c.form/branches/gocept-invariants/src/z3c/form/field.py	2007-09-27 08:28:42 UTC (rev 80193)
@@ -202,9 +202,12 @@
 
         # Step 2: Validate the individual schemas and collect errors
         errors = ()
+        content = self.content
+        if self.ignoreContext:
+            content = None
         for schema, fieldData in schemaData.items():
             validator = zope.component.getMultiAdapter(
-                (self.content, self.request, self.form, schema, self),
+                (content, self.request, self.form, schema, self),
                 interfaces.IManagerValidator)
             errors += validator.validate(fieldData)
 

Modified: z3c.form/branches/gocept-invariants/src/z3c/form/validator.py
===================================================================
--- z3c.form/branches/gocept-invariants/src/z3c/form/validator.py	2007-09-27 08:24:25 UTC (rev 80192)
+++ z3c.form/branches/gocept-invariants/src/z3c/form/validator.py	2007-09-27 08:28:42 UTC (rev 80193)
@@ -99,7 +99,11 @@
         # Try to get the value for the field
         value = data.get(name, data)
         if value is data:
-            raise NoInputData(name)
+            if self.__context__ is None:
+                raise NoInputData(name)
+            dm = zope.component.getMultiAdapter(
+                (self.__context__, field), interfaces.IDataManager)
+            value = dm.get()
         # Optimization: Once we now we have a good value, set it as an
         # attribute for faster access.
         setattr(self, name, value)

Modified: z3c.form/branches/gocept-invariants/src/z3c/form/validator.txt
===================================================================
--- z3c.form/branches/gocept-invariants/src/z3c/form/validator.txt	2007-09-27 08:24:25 UTC (rev 80192)
+++ z3c.form/branches/gocept-invariants/src/z3c/form/validator.txt	2007-09-27 08:28:42 UTC (rev 80193)
@@ -205,6 +205,7 @@
 or an object implementing the schema:
 
   >>> class Person(object):
+  ...     zope.interface.implements(IPerson)
   ...     login = u'srichter'
   ...     email = u'srichter at foo.com'
   >>> stephan = Person()
@@ -249,7 +250,7 @@
   ...     print e.__class__.__name__ + ':', e
   Invalid: Email too long.
 
-To register the custom validator only for this schema, we hace to use the
+To register the custom validator only for this schema, we have to use the
 discriminator generator again.
 
   >>> from z3c.form import util
@@ -324,3 +325,22 @@
 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.
+
+Validation of interface variants when not all fields are displayed in form
+--------------------------------------------------------------------------
+
+We need to register the data manager to access the data on the context object:
+
+  >>> from z3c.form import datamanager
+  >>> zope.component.provideAdapter(datamanager.AttributeField)
+
+Sometimes you might leave out fields in the form which need to compute the
+invariant. An exception should be raised. The data wrapper is used to test
+the invariants and looks up values on the context object that are left out in
+the form.
+
+  >>> invariants = validator.InvariantsValidator(
+  ...     stephan, None, None, IPerson, None)
+  >>> invariants.validate({'email': 'foo at bar.com'})
+  (<zope.interface.exceptions.Invalid instance at ...>,)
+



More information about the Checkins mailing list