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
This commit is contained in:
Jakob Ketterl 2021-05-12 16:22:45 +02:00
parent 002827cbf4
commit 1020c9bac9
2 changed files with 8 additions and 8 deletions

View File

@ -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):
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} if self.id in data else {}
return {self.id: value}
return {}
def getLabel(self):
return self.label

View File

@ -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: