[Checkins] SVN: zope.formlib/trunk/ Added `ignoreContext` attribute to form classes to control whether `checkInvariants` takes the context of the form into account when checking interface invariants.

Michael Howitz cvs-admin at zope.org
Thu Mar 15 09:26:37 UTC 2012


Log message for revision 124601:
  Added `ignoreContext` attribute to form classes to control whether `checkInvariants` takes the context of the form into account when checking interface invariants.
  

Changed:
  U   zope.formlib/trunk/CHANGES.txt
  U   zope.formlib/trunk/src/zope/formlib/form.py
  U   zope.formlib/trunk/src/zope/formlib/tests/test_formlib.py

-=-
Modified: zope.formlib/trunk/CHANGES.txt
===================================================================
--- zope.formlib/trunk/CHANGES.txt	2012-03-15 09:21:12 UTC (rev 124600)
+++ zope.formlib/trunk/CHANGES.txt	2012-03-15 09:26:33 UTC (rev 124601)
@@ -5,9 +5,15 @@
 4.1.1 (unreleased)
 ==================
 
-- Nothing changed yet.
+- Added `ignoreContext` attribute to form classes to control whether
+  `checkInvariants` takes the context of the form into account when
+  checking interface invariants.
 
+  By default `ignoreContext` is set to ``False``.  On the `AddForm` it is
+  ``True`` by default because the context of this form is naturally not
+  suitable as context for the interface invariant.
 
+
 4.1.0 (2012-03-15)
 ==================
 

Modified: zope.formlib/trunk/src/zope/formlib/form.py
===================================================================
--- zope.formlib/trunk/src/zope/formlib/form.py	2012-03-15 09:21:12 UTC (rev 124600)
+++ zope.formlib/trunk/src/zope/formlib/form.py	2012-03-15 09:26:33 UTC (rev 124601)
@@ -479,7 +479,7 @@
                     raise NoInputData(name)
                 # The value is not in the form look it up on the context:
                 field = schema[name]
-                adapted_context = schema(context)
+                adapted_context = schema(context, None)
                 if IField.providedBy(field):
                     value = field.get(adapted_context)
                 elif (zope.interface.interfaces.IAttribute.providedBy(field)
@@ -743,6 +743,8 @@
 
     errors = ()
 
+    ignoreContext = False
+
     interface.implements(interfaces.IForm)
 
     def setPrefix(self, prefix):
@@ -755,8 +757,12 @@
             form=self, adapters=self.adapters, ignore_request=ignore_request)
 
     def validate(self, action, data):
+        if self.ignoreContext:
+            context = None
+        else:
+            context = self.context
         return (getWidgetsData(self.widgets, self.prefix, data)
-                + checkInvariants(self.form_fields, data, self.context))
+                + checkInvariants(self.form_fields, data, context))
 
     template = namedtemplate.NamedTemplate('default')
 
@@ -891,6 +897,8 @@
 
 class AddFormBase(FormBase):
 
+    ignoreContext = True
+
     interface.implements(interfaces.IAddFormCustomization,
                          zope.component.interfaces.IFactory)
 

Modified: zope.formlib/trunk/src/zope/formlib/tests/test_formlib.py
===================================================================
--- zope.formlib/trunk/src/zope/formlib/tests/test_formlib.py	2012-03-15 09:21:12 UTC (rev 124600)
+++ zope.formlib/trunk/src/zope/formlib/tests/test_formlib.py	2012-03-15 09:26:33 UTC (rev 124601)
@@ -630,9 +630,11 @@
 """
 
 
-def checkInvariants_falls_back_to_context_if_value_not_in_form():
+def validate_respects_ignoreContext_setting_on_form_when_checking_invariants():
     """
-`checkInvariants` is able to access the values from the form and the context to
+The `validate` method of the form respects the value of
+``self.ignoreContext`` when calling `checkInvariants`. `checkInvariants` is
+able to access the values from the form and the context (if not ignored) to
 make sure invariants are not violated:
 
     >>> class IFlexMaximum(zope.interface.Interface):
@@ -649,32 +651,51 @@
     ...     max = 10
     ...     value = 7
 
-    >>> class ValueEditForm(zope.formlib.form.EditForm):
+    >>> class ValueForm(zope.formlib.form.FormBase):
+    ...     ignoreContext = False
     ...     form_fields = zope.formlib.form.FormFields(
     ...         IFlexMaximum).omit('max')
+    ...
+    ...     @zope.formlib.form.action("Apply")
+    ...     def handle_apply(self, action, data):
+    ...         pass
 
-If the value entered in the example form is bigger than the maximum the
-interface invariant triggers an error:
+`checkInvariants` is able to access the value on the context, so the
+interface invariant triggers an error message:
 
     >>> from zope.publisher.browser import TestRequest
     >>> request = TestRequest(
     ...     form={'form.value': 42, 'form.actions.apply': '1'})
-    >>> form = ValueEditForm(Content(), request)
+    >>> form = ValueForm(Content(), request)
     >>> form.update()
     >>> form.errors
     (Invalid('value bigger than max',),)
 
-If the value is below the maximum no error occures:
+`checkInvariants` if the entered value is small enough, the error message is
+not triggered:
 
+    >>> from zope.publisher.browser import TestRequest
     >>> request = TestRequest(
     ...     form={'form.value': 8, 'form.actions.apply': '1'})
-    >>> form = ValueEditForm(Content(), request)
+    >>> form = ValueForm(Content(), request)
     >>> form.update()
     >>> form.errors
     ()
+
+The error is not triggered, too,  if ``ignoreContext`` is set to ``True`` as
+`checkInvariants` does not access the context then:
+
+    >>> request = TestRequest(
+    ...     form={'form.value': 42, 'form.actions.apply': '1'})
+    >>> form = ValueForm(Content(), request)
+    >>> form.ignoreContext = True
+    >>> form.update()
+    >>> form.errors
+    ()
 """
 
 
+
 def FormData___getattr___handles_zope_interrface_attributes_correctly():
     """
 `FormData.__getattr__` reads objects defined as zope.interface.Attribute in
@@ -693,7 +714,7 @@
 """
 
 
-def FormData___getattr___raises_exception_if_unknown_how_to_access_value():
+def FormData___getattr___raises_NoInputData_if_unknown_how_to_access_value():
     """
 `FormData.__getattr__` raises an exception if it cannot determine how to
 read the object from context:



More information about the checkins mailing list