Skip to content

Commit

Permalink
Add MultiWidget error example
Browse files Browse the repository at this point in the history
  • Loading branch information
Actionb committed May 11, 2023
1 parent 64f4f1f commit 6555d75
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions testapp/forms/multivalue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django import forms


class PhoneWidget(forms.MultiWidget):

def decompress(self, value):
if value is None:
return ['' * 3]
return value.split('-')


class PhoneField(forms.MultiValueField):

widget = PhoneWidget(widgets=[forms.TextInput, forms.TextInput, forms.TextInput])

def __init__(self, **kwargs):
super().__init__(fields=(forms.CharField(), forms.CharField(), forms.CharField()), **kwargs)

def compress(self, data_list):
return '-'.join(data_list)


class MultiValueForm(forms.Form):
name = forms.CharField()
phone_number = PhoneField(label="Phone Number")
4 changes: 4 additions & 0 deletions testapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from testapp.forms.county import CountyForm
from testapp.forms.customer import CustomerCollection
from testapp.forms.moon import MoonForm, MoonCalendarRenderer
from testapp.forms.multivalue import MultiValueForm
from testapp.forms.opinion import OpinionForm
from testapp.forms.person import ButtonActionsForm, SimplePersonForm, sample_person_data, ModelPersonForm
from testapp.forms.poll import ModelPollForm, PollCollection
Expand Down Expand Up @@ -562,6 +563,9 @@ class CompleteForm(FormMixin, forms.Form):
template_name='testapp/button-actions.html',
extra_context={'click_actions': 'clearErrors -> disable -> spinner -> submit -> okay(1500) -> proceed !~ enable -> bummer(9999)'},
), kwargs={'group': 'button', 'index': 20}, name='button-actions'),
path('multi-value', DemoFormView.as_view(
form_class=MultiValueForm
), kwargs={'index': 1}, name='multi-value')
]

# this creates permutations of forms to show how to withhold which feedback
Expand Down

0 comments on commit 6555d75

Please sign in to comment.