Skip to content

Commit

Permalink
Merge pull request #4128 from open-formulieren/feature/add-validation…
Browse files Browse the repository at this point in the history
…-feature-flag

Add validation feature flag
  • Loading branch information
sergei-maertens authored Apr 8, 2024
2 parents 74cccd1 + cf664f7 commit 8b36990
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/openforms/config/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class GlobalConfigurationAdmin(TranslationAdmin, SingletonModelAdmin):
"enable_demo_plugins",
"allow_empty_initiator",
"payment_order_id_prefix",
"enable_backend_formio_validation",
),
},
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.11 on 2024-04-08 09:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("config", "0055_remove_globalconfiguration_enable_react_formio_builder"),
]

operations = [
migrations.AddField(
model_name="globalconfiguration",
name="enable_backend_formio_validation",
field=models.BooleanField(
default=True, verbose_name="enabled backend formio validation"
),
),
]
5 changes: 5 additions & 0 deletions src/openforms/config/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ class GlobalConfiguration(SingletonModel):
help_text=_("When enabled, information about the used SDK is displayed."),
)

enable_backend_formio_validation = models.BooleanField(
_("enabled backend formio validation"),
default=True,
)

# Removing data configurations
successful_submissions_removal_limit = models.PositiveIntegerField(
_("successful submission removal limit"),
Expand Down
14 changes: 11 additions & 3 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.config.models import GlobalConfiguration
from openforms.formio.service import build_serializer

from ..form_logic import check_submission_logic
Expand Down Expand Up @@ -47,8 +48,17 @@ def validate_contains_blocked_steps(self, value):
)

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

def _run_formio_validation(self) -> None:
# Check feature flag to opt out of formio validation first.
config = GlobalConfiguration.get_solo()
assert isinstance(config, GlobalConfiguration)
if not config.enable_backend_formio_validation:
return

submission: Submission = self.context["submission"]
formio_validation_errors = []

data = submission.data
Expand All @@ -74,8 +84,6 @@ def validate(self, attrs: dict):
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 @@ -117,6 +117,41 @@ def test_component_level_validation(self):
]
self.assertIn("steps.0.data.firstName", invalid_param_names)

@patch(
"openforms.submissions.api.validation.GlobalConfiguration.get_solo",
return_value=GlobalConfiguration(enable_backend_formio_validation=False),
)
def test_component_level_validation_disabled_via_feature_flag(self, m_solo):
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_200_OK)

@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 8b36990

Please sign in to comment.