diff --git a/src/openforms/forms/tests/test_api_form_plugin_options.py b/src/openforms/forms/tests/test_api_form_plugin_options.py index 2f1d3324b2..88f10d066b 100644 --- a/src/openforms/forms/tests/test_api_form_plugin_options.py +++ b/src/openforms/forms/tests/test_api_form_plugin_options.py @@ -272,10 +272,4 @@ def test_cannot_overwrite_only_registration_email_html_template(self): data = response.json() error = data["invalidParams"][0]["reason"] - self.assertEqual( - error, - _( - "The fields {fields} must all have a non-empty value as soon as one of them " - "does." - ).format(fields="email_content_template_html, email_content_template_text"), - ) + self.assertEqual(error, _("You must also specify text along with the html.")) diff --git a/src/openforms/registrations/contrib/email/config.py b/src/openforms/registrations/contrib/email/config.py index 2fb4ca3a5a..499d742e5f 100644 --- a/src/openforms/registrations/contrib/email/config.py +++ b/src/openforms/registrations/contrib/email/config.py @@ -5,7 +5,6 @@ from rest_framework import serializers from typing_extensions import NotRequired -from openforms.api.validators import AllOrNoneTruthyFieldsValidator from openforms.emails.validators import URLSanitationValidator from openforms.formio.api.fields import FormioVariableKeyField from openforms.template.validators import DjangoTemplateValidator @@ -123,13 +122,33 @@ class EmailOptionsSerializer(JsonSchemaSerializerMixin, serializers.Serializer): ], ) - class Meta: - validators = [ - AllOrNoneTruthyFieldsValidator( - "email_content_template_html", - "email_content_template_text", - ), - ] + def validate(self, attrs: Options) -> Options: + html = attrs.get("email_content_template_html") + text = attrs.get("email_content_template_text") + + if not html and not text: + return attrs + + if html and not text: + raise serializers.ValidationError( + { + "email_content_template_text": _( + "You must also specify text along with the html." + ), + }, + code="required", + ) + if text and not html: + raise serializers.ValidationError( + { + "email_content_template_html": _( + "You must also specify html along with the text." + ), + }, + code="required", + ) + + return attrs # sanity check for development - keep serializer and type definitions in sync diff --git a/src/openforms/registrations/contrib/email/tests/test_serializers.py b/src/openforms/registrations/contrib/email/tests/test_serializers.py new file mode 100644 index 0000000000..e3a1cd5a6e --- /dev/null +++ b/src/openforms/registrations/contrib/email/tests/test_serializers.py @@ -0,0 +1,48 @@ +from django.test import TestCase + +from ..config import EmailOptionsSerializer + + +class EmailOptionsSerializerTest(TestCase): + def test_email_and_text_content_fields(self): + with self.subTest("both fields missing"): + serializer = EmailOptionsSerializer( + data={ + "to_emails": ["user@example.com"], + "email_content_template_html": "", + "email_content_template_text": "", + }, + context={"request": None}, + ) + + self.assertTrue(serializer.is_valid()) + + with self.subTest("text is missing"): + serializer = EmailOptionsSerializer( + data={ + "to_emails": ["user@example.com"], + "email_content_template_html": "
some text
", + "email_content_template_text": "", + }, + context={"request": None}, + ) + + self.assertFalse(serializer.is_valid()) + self.assertEqual( + serializer.errors["email_content_template_text"][0].code, "required" + ) + + with self.subTest("html is missing"): + serializer = EmailOptionsSerializer( + data={ + "to_emails": ["user@example.com"], + "email_content_template_html": "", + "email_content_template_text": "some text", + }, + context={"request": None}, + ) + + self.assertFalse(serializer.is_valid()) + self.assertEqual( + serializer.errors["email_content_template_html"][0].code, "required" + )