From 1020c9bac9962734ff3dadba74882665a8e47414 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Wed, 12 May 2021 16:22:45 +0200 Subject: [PATCH] improve form validation * don't ingore errors in optional fields * don't attempt parsing if key is not present in upload * force display of fields with errors --- owrx/form/input/__init__.py | 10 ++++++---- owrx/form/section.py | 6 ++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/owrx/form/input/__init__.py b/owrx/form/input/__init__.py index 840985a..a0bacfb 100644 --- a/owrx/form/input/__init__.py +++ b/owrx/form/input/__init__.py @@ -90,10 +90,12 @@ class Input(ABC): return self.bootstrap_decorate(self.render_input_group(self.converter.convert_to_form(value), error)) def parse(self, data): - value = self.converter.convert_from_form(data[self.id][0]) - if self.validator is not None: - self.validator.validate(self.id, value) - return {self.id: value} if self.id in data else {} + if self.id in data: + value = self.converter.convert_from_form(data[self.id][0]) + if self.validator is not None: + self.validator.validate(self.id, value) + return {self.id: value} + return {} def getLabel(self): return self.label diff --git a/owrx/form/section.py b/owrx/form/section.py index 3c53cf2..1eb9b9e 100644 --- a/owrx/form/section.py +++ b/owrx/form/section.py @@ -103,8 +103,8 @@ class OptionalSection(Section): def render(self, data, errors): indexed_inputs = {input.id: input for input in self.inputs} - visible_keys = set(self.mandatory + [k for k in self.optional if k in data]) - optional_keys = set(k for k in self.optional if k not in data) + visible_keys = set(self.mandatory + [k for k in self.optional if k in data or k in errors]) + optional_keys = set(k for k in self.optional if k not in data and k not in errors) self.inputs = [input for k, input in indexed_inputs.items() if k in visible_keys] for input in self.inputs: if self._is_optional(input): @@ -117,8 +117,6 @@ class OptionalSection(Section): def parse(self, data): data, errors = super().parse(data) - # filter out errors for optional fields - errors = [e for e in errors if e.getKey() not in self.optional or e.getKey() in data] # remove optional keys if they have been removed from the form by setting them to None for k in self.optional: if k not in data: