From dafee9ad7285989862b3e9bdd3c1baad098c0c0c Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Mon, 26 Apr 2021 10:17:01 +0200 Subject: [PATCH] Plugin API: Allow to add validators to checkout form fields --- src/pretix/base/views/mixins.py | 4 ++++ src/pretix/presale/checkoutflow.py | 4 ++++ src/pretix/presale/signals.py | 9 +++++---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/pretix/base/views/mixins.py b/src/pretix/base/views/mixins.py index 1d974d398..2be6187fb 100644 --- a/src/pretix/base/views/mixins.py +++ b/src/pretix/base/views/mixins.py @@ -98,12 +98,16 @@ class BaseQuestionsViewMixin: question_field.initial = overrides[question_field.question.identifier]['initial'] if 'disabled' in overrides[question_field.question.identifier]: question_field.disabled = overrides[question_field.question.identifier]['disabled'] + if 'validators' in overrides[question_field.question.identifier]: + question_field.validators += overrides[question_field.question.identifier]['validators'] else: if question_name in overrides: if 'initial' in overrides[question_name]: question_field.initial = overrides[question_name]['initial'] if 'disabled' in overrides[question_name]: question_field.disabled = overrides[question_name]['disabled'] + if 'validators' in overrides[question_name]: + question_field.validators += overrides[question_name]['validators'] if len(form.fields) > 0: formlist.append(form) diff --git a/src/pretix/presale/checkoutflow.py b/src/pretix/presale/checkoutflow.py index 91b1b253a..cc70b94bc 100644 --- a/src/pretix/presale/checkoutflow.py +++ b/src/pretix/presale/checkoutflow.py @@ -498,6 +498,8 @@ class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep): for fname, val in overrides.items(): if 'disabled' in val and fname in f.fields: f.fields[fname].disabled = val['disabled'] + if 'validators' in val and fname in f.fields: + f.fields[fname].validators += val['validators'] return f @@ -564,6 +566,8 @@ class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep): for fname, val in overrides.items(): if 'disabled' in val and fname in f.fields: f.fields[fname].disabled = val['disabled'] + if 'validators' in val and fname in f.fields: + f.fields[fname].validators += val['validators'] return f diff --git a/src/pretix/presale/signals.py b/src/pretix/presale/signals.py index 3ca3723a8..703aaf47e 100644 --- a/src/pretix/presale/signals.py +++ b/src/pretix/presale/signals.py @@ -229,8 +229,8 @@ contact_form_fields_overrides = EventPluginSignal( This signal allows you to override fields of the contact form that is presented during checkout and by default only asks for the email address. It is also being used for the invoice address form. You are supposed to return a dictionary of dictionaries with globally unique keys. The -value-dictionary should contain one or more of the following keys: ``initial``, ``disabled``. The -key of the dictionary should be the name of the form field. +value-dictionary should contain one or more of the following keys: ``initial``, ``disabled``, +``validators``. The key of the dictionary should be the name of the form field. As with all plugin signals, the ``sender`` keyword argument will contain the event. A ``request`` argument will contain the request object. The ``order`` argument is ``None`` during the checkout @@ -260,8 +260,9 @@ question_form_fields_overrides = EventPluginSignal( This signal allows you to override fields of the questions form that is presented during checkout and by default only asks for the questions configured in the backend. You are supposed to return a dictionary of dictionaries with globally unique keys. The value-dictionary should contain one or -more of the following keys: ``initial``, ``disabled``. The key of the dictionary should not be the -question's form field name (``question_n``) but rather the questions ``identifier``. +more of the following keys: ``initial``, ``disabled``, ``validators``. The key of the dictionary +should be the form field name for system fields (e.g. ``company``), or the question's ``identifier`` +for user-defined questions. The ``position`` keyword argument will contain a ``CartPosition`` or ``OrderPosition`` object.