Skip to content

Commit

Permalink
Make the form field validation configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Nov 21, 2019
1 parent d8b9cd6 commit 8983a37
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
34 changes: 32 additions & 2 deletions form_designer/default_field_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,70 @@
from django.utils.translation import ugettext_lazy as _


def disallow_choices(field):
if field.choices:
raise forms.ValidationError(
_("You can't specify choices for %s fields") % field.type
)


def require_choices(field):
if not field.choices:
raise forms.ValidationError(
_("Please specify choices for %s fields") % field.type
)


FIELD_TYPES = [
{"type": "text", "verbose_name": _("text"), "field": forms.CharField},
{"type": "email", "verbose_name": _("e-mail address"), "field": forms.EmailField},
{
"type": "text",
"verbose_name": _("text"),
"field": forms.CharField,
"clean_field": [disallow_choices],
},
{
"type": "email",
"verbose_name": _("e-mail address"),
"field": forms.EmailField,
"clean_field": [disallow_choices],
},
{
"type": "longtext",
"verbose_name": _("long text"),
"field": partial(forms.CharField, widget=forms.Textarea),
"clean_field": [disallow_choices],
},
{
"type": "checkbox",
"verbose_name": _("checkbox"),
"field": partial(forms.BooleanField, required=False),
"clean_field": [disallow_choices],
},
{
"type": "select",
"verbose_name": _("select"),
"field": partial(forms.ChoiceField, required=False),
"clean_field": [require_choices],
},
{
"type": "radio",
"verbose_name": _("radio"),
"field": partial(forms.ChoiceField, widget=forms.RadioSelect),
"clean_field": [require_choices],
},
{
"type": "multiple-select",
"verbose_name": _("multiple select"),
"field": partial(
forms.MultipleChoiceField, widget=forms.CheckboxSelectMultiple
),
"clean_field": [require_choices],
},
{
"type": "hidden",
"verbose_name": _("hidden"),
"field": partial(forms.CharField, widget=forms.HiddenInput),
"clean_field": [disallow_choices],
},
]

Expand Down
15 changes: 4 additions & 11 deletions form_designer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,13 @@ def __str__(self):

def clean(self):
try:
field_type = self.get_type()
except KeyError:
cfg = next((type for type in FIELD_TYPES if type["type"] == self.type))
except StopIteration:
# Fine. The model will not validate anyway.
return

if self.choices and not isinstance(field_type, forms.ChoiceField):
raise forms.ValidationError(
_("You can't specify choices for %s fields") % self.type
)

elif not self.choices and isinstance(field_type, forms.ChoiceField):
raise forms.ValidationError(
_("Please specify choices for %s fields") % self.type
)
for fn in cfg.get("clean_field", ()):
fn(self)

def get_choices(self):
def get_tuple(value):
Expand Down

0 comments on commit 8983a37

Please sign in to comment.