- commit
- 6231f8b0f405adf4c8c5a959398cfdb820f438a1
- parent
- 6b1d3164cad712a9442c6a91b4c0bb0e93025da3
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2023-04-19 04:49
example: add form validation
Diffstat
| M | example/forms.py | 5 | +++++ |
| M | example/templates/form.html | 8 | ++++++++ |
| M | example/views.py | 6 | ++++++ |
3 files changed, 19 insertions, 0 deletions
diff --git a/example/forms.py b/example/forms.py
@@ -28,3 +28,8 @@ class ExampleForm(forms.Form): 28 28 ) 29 29 date = forms.DateField(widget=forms.SelectDateWidget()) 30 30 date_time = forms.SplitDateTimeField() -1 31 -1 32 def clean(self): -1 33 cleaned_data = super().clean() -1 34 self.add_error(None, 'This is a non-field error') -1 35 return cleaned_data
diff --git a/example/templates/form.html b/example/templates/form.html
@@ -8,6 +8,14 @@ 8 8 </head> 9 9 <body> 10 10 <div class="container my-4"> -1 11 <form class="mb-4" method="GET"> -1 12 {% if view.request.GET.validate %} -1 13 <button name="validate" value="" class="btn btn-primary">Disable validation</button> -1 14 {% else %} -1 15 <button name="validate" value="1" class="btn btn-primary">Enable validation</button> -1 16 {% endif %} -1 17 </form> -1 18 11 19 <form> 12 20 {{ form }} 13 21 <button class="btn btn-primary">Submit</button>
diff --git a/example/views.py b/example/views.py
@@ -6,3 +6,9 @@ from .forms import ExampleForm 6 6 class ExampleFormView(FormView): 7 7 form_class = ExampleForm 8 8 template_name = 'form.html' -1 9 -1 10 def get_form_kwargs(self): -1 11 kwargs = super().get_form_kwargs() -1 12 if self.request.GET.get('validate'): -1 13 kwargs['data'] = {} -1 14 return kwargs