Skip to content

Commit

Permalink
Merge pull request #3998 from open-formulieren/feature/3977-apply-for…
Browse files Browse the repository at this point in the history
…mio-component-validations

Apply formio component validations during submission complete
  • Loading branch information
sergei-maertens authored Mar 13, 2024
2 parents 78dea0c + 7ea068c commit bed8118
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/openforms/submissions/api/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from rest_framework import serializers

from openforms.api.utils import mark_experimental
from openforms.formio.service import build_serializer

from ..form_logic import check_submission_logic
from ..models import Submission, SubmissionStep
Expand Down Expand Up @@ -45,6 +46,31 @@ def validate_contains_blocked_steps(self, value):
)
)

def validate(self, attrs: dict):
submission: Submission = self.context["submission"]

formio_validation_errors = []

data = submission.data
for step in submission.steps:
errors = {}
assert step.form_step
components = step.form_step.form_definition.configuration["components"]

step_data_serializer = build_serializer(components, data=data)
if not step_data_serializer.is_valid():
errors = step_data_serializer.errors

if errors:
formio_validation_errors.append({"data": errors})
else:
formio_validation_errors.append(None)

if any(formio_validation_errors):
raise serializers.ValidationError({"steps": formio_validation_errors})

return attrs

def save(self, **kwargs):
submission = self.context["submission"]
submission.privacy_policy_accepted = True
Expand Down
35 changes: 35 additions & 0 deletions src/openforms/submissions/tests/test_submission_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,41 @@ def test_all_required_steps_validated(self):
],
)

def test_component_level_validation(self):
submission = SubmissionFactory.create(
form__generate_minimal_setup=True,
form__formstep__form_definition__configuration={
"components": [
{
"type": "textfield",
"key": "firstName",
"label": "First name",
"validate": {
"required": True,
"maxLength": 20,
},
}
]
},
)
SubmissionStepFactory.create(
submission=submission,
form_step=submission.form.formstep_set.get(),
data={
"firstName": "this value is longer than twenty characters and should not validate"
},
)
self._add_submission_to_session(submission)
endpoint = reverse("api:submission-complete", kwargs={"uuid": submission.uuid})

response = self.client.post(endpoint, {"privacy_policy_accepted": True})

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
invalid_param_names = [
param["name"] for param in response.json()["invalidParams"]
]
self.assertIn("steps.0.data.firstName", invalid_param_names)

@patch("openforms.submissions.api.mixins.on_post_submission_event")
@freeze_time("2020-12-11T10:53:19+01:00")
def test_complete_submission(self, mock_on_post_submission_event):
Expand Down

0 comments on commit bed8118

Please sign in to comment.