Plugin API: Allow to add validators to checkout form fields

This commit is contained in:
Raphael Michel
2021-04-26 10:17:01 +02:00
parent 967c641e14
commit dafee9ad72
3 changed files with 13 additions and 4 deletions

View File

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

View File

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

View File

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