Added check against validation errors in AJAX forms
authorThierry Florac <thierry.florac@onf.fr>
Tue, 13 Mar 2018 16:33:29 +0100
changeset 88 f1b5a6216f2b
parent 87 cfd3fe45ed6b
child 89 8ed90e3c8de8
Added check against validation errors in AJAX forms
src/pyams_form/form.py
--- a/src/pyams_form/form.py	Sun Mar 11 11:23:25 2018 +0100
+++ b/src/pyams_form/form.py	Tue Mar 13 16:33:29 2018 +0100
@@ -358,13 +358,24 @@
         except ValidationError as error:
             # This error can occur with file-type inputs
             registry = self.request.registry
-            widget = self.widgets[error.args[-1]]
-            view = registry.getMultiAdapter((error, self.request, widget, widget.field, self, self.context),
-                                            IErrorViewSnippet)
-            view.update()
-            widget.error = view
-            errors = (view,)
-            return self.get_ajax_errors(errors)
+            for form in self.forms:
+                try:
+                    widget = form.widgets[error.args[-1]]
+                except KeyError:
+                    continue
+                else:
+                    view = registry.getMultiAdapter((error, self.request, widget, widget.field, self, self.context),
+                                                    IErrorViewSnippet)
+                    view.update()
+                    widget.error = view
+                    errors = (view,)
+                    return self.get_ajax_errors(errors)
+            # Fallback when you can't find widget
+            translate = self.request.localizer.translate
+            return {
+                'status': 'error',
+                'message': translate(self.formErrorsMessage)
+            }
         else:
             return self.get_ajax_output(result)
 
@@ -459,22 +470,32 @@
         if errors or self.errors:
             return self.get_ajax_errors()
         # update form content
+        translate = self.request.localizer.translate
         try:
             changes = self.applyChanges(data) or {}
         except ValidationError as error:
             # This error can occur with file-type inputs
             registry = self.request.registry
-            widget = form.widgets[error.args[-1]]
-            view = registry.getMultiAdapter((error, self.request, widget, widget.field, self, self.context),
-                                            IErrorViewSnippet)
-            view.update()
-            widget.error = view
-            errors = (view,)
-            return self.get_ajax_errors(errors)
+            for form in self.forms:
+                try:
+                    widget = form.widgets[error.args[-1]]
+                except KeyError:
+                    continue
+                else:
+                    view = registry.getMultiAdapter((error, self.request, widget, widget.field, self, self.context),
+                                                    IErrorViewSnippet)
+                    view.update()
+                    widget.error = view
+                    errors = (view,)
+                    return self.get_ajax_errors(errors)
+            # Fallback when you can't find widget
+            return {
+                'status': 'error',
+                'message': translate(self.formErrorsMessage)
+            }
         else:
             # check JSON output
             output = self.get_ajax_output(changes)
-            translate = self.request.localizer.translate
             status = output.get('status')
             message = output.get('message')
             if not changes: