Skip to content

Commit

Permalink
Pass additional data to validate
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Dec 1, 2019
1 parent 89d7485 commit f5cc614
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Next version

* Fixed the config fieldsets code to work when using Django 3.0.
* Added an unit test and docs for the ``"validate"`` config option.
* Passed additional data to the ``"validate"`` config option. Not
accepting arbitrary keyword arguments is now deprecated.
* Changed the forms administration interface to show all form options
from the beginning. This requires a change to the ``form_fields``
configuration option: Instead of a list it has to be a callable
Expand Down
13 changes: 12 additions & 1 deletion form_designer/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import OrderedDict
from functools import partial
import json
import warnings

Expand All @@ -10,6 +11,7 @@
from django.db import models
from django.db.models.fields import BLANK_CHOICE_DASH
from django.utils.html import format_html, format_html_join
from django.utils.inspect import func_accepts_kwargs
from django.utils.module_loading import import_string
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -106,9 +108,18 @@ def form_class(self):
cfg = dict(self.CONFIG_OPTIONS)
for key, config in self.config.items():
try:
validators.append(cfg[key]["validate"])
validator = cfg[key]["validate"]
except KeyError:
pass
else:
if func_accepts_kwargs(validator):
validator = partial(validator, config=config, model_instance=self)
else:
warnings.warn(
"validate of %r should accept **kwargs" % (key,),
DeprecationWarning,
)
validators.append(validator)

class Form(forms.Form):
def clean(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from form_designer.models import Form, FormSubmission, FIELD_TYPES


def validate_honeypot(form, data):
def validate_honeypot(form, data, **kwargs):
if data.get("honeypot"):
raise forms.ValidationError("Hello honeypot")

Expand Down

0 comments on commit f5cc614

Please sign in to comment.