Handle multiple errors snippets (used by ObjectWidget)
authorThierry Florac <tflorac@ulthar.net>
Sun, 26 Feb 2017 10:37:14 +0100
changeset 47 d88cb7dfabfc
parent 46 95e1e28acb19
child 48 13a0658bdc9c
Handle multiple errors snippets (used by ObjectWidget)
src/pyams_form/form.py
--- a/src/pyams_form/form.py	Sun Feb 26 10:36:27 2017 +0100
+++ b/src/pyams_form/form.py	Sun Feb 26 10:37:14 2017 +0100
@@ -28,7 +28,7 @@
 from pyams_template.interfaces import IContentTemplate, ILayoutTemplate
 from pyams_utils.interfaces import FORBIDDEN_PERMISSION
 from pyramid_chameleon.interfaces import IChameleonTranslate
-from z3c.form.interfaces import DISPLAY_MODE, IErrorViewSnippet
+from z3c.form.interfaces import DISPLAY_MODE, IErrorViewSnippet, IMultipleErrors
 from zope.publisher.interfaces.browser import IBrowserRequest
 
 # import packages
@@ -255,22 +255,39 @@
             if isinstance(error, Exception):
                 error = registry.getMultiAdapter((error, self.request, None, None, self, self.request),
                                                  IErrorViewSnippet)
-
             error.update()
-            if hasattr(error, 'widget'):
-                widget = error.widget
-                if widget is not None:
-                    errors.setdefault('widgets', []).append({'name': widget.name,
-                                                             'label': translate(widget.label),
-                                                             'message': translate(error.message)})
+            if IMultipleErrors.providedBy(error.error):
+                for inner_error in error.error.errors:
+                    if hasattr(inner_error, 'widget'):
+                        widget = inner_error.widget
+                        if widget is not None:
+                            errors.setdefault('widgets', []).append({ 'name': widget.name,
+                                                                      'label': translate(widget.label),
+                                                                      'message': translate(inner_error.message) })
+                        else:
+                            errors.setdefault('messages', []).append({ 'message': translate(inner_error.message) })
+                    else:
+                        errors.setdefault('messages', []).append(translate(inner_error.message))
+            else:
+                if hasattr(error, 'widget'):
+                    widget = error.widget
+                    if widget is not None:
+                        errors.setdefault('widgets', []).append({'name': widget.name,
+                                                                 'label': translate(widget.label),
+                                                                 'message': translate(error.message)})
+                    else:
+                        errors.setdefault('messages', []).append({'message': translate(error.message)})
                 else:
-                    errors.setdefault('messages', []).append({'message': translate(error.message)})
-            else:
-                errors.setdefault('messages', []).append(translate(error.message))
+                    errors.setdefault('messages', []).append(translate(error.message))
         return errors
 
     def get_ajax_output(self, changes):
         """Extract AJAX POST output"""
+        for form in self.subforms + self.tabforms:
+            try:
+                return form.get_ajax_output(changes)
+            except NotImplementedError:
+                pass
         raise NotImplementedError
 
 
@@ -334,6 +351,13 @@
         return self.get_ajax_output(result)
 
     def get_ajax_output(self, changes):
+        for form in self.subforms + self.tabforms:
+            try:
+                output = form.get_ajax_output(changes)
+                if output is not None:
+                    return output
+            except NotImplementedError:
+                pass
         return {'status': 'reload',
                 'location': self.nextURL()}
 
@@ -415,6 +439,13 @@
         return self.get_ajax_output(changes)
 
     def get_ajax_output(self, changes):
+        for form in self.subforms + self.tabforms:
+            try:
+                output = form.get_ajax_output(changes)
+                if output is not None:
+                    return output
+            except NotImplementedError:
+                pass
         if changes:
             status = 'success'
             message = self.successMessage