From 32555f7a1c599c139e0f26c22907c9612af2e015 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Fri, 3 Feb 2017 13:14:35 +0100 Subject: [PATCH] Added hiddenfield --- CHANGELOG.txt | 1 + CONTRIBUTORS.rst | 1 + docs/releases/2.0.rst | 2 ++ .../migrations/0021_hidden_form_field.py | 30 +++++++++++++++++++ wagtail/wagtailforms/forms.py | 4 +++ wagtail/wagtailforms/models.py | 1 + wagtail/wagtailforms/tests/test_forms.py | 10 +++++++ 7 files changed, 49 insertions(+) create mode 100644 wagtail/tests/testapp/migrations/0021_hidden_form_field.py diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 288f7e1feb..2d47093a05 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -5,6 +5,7 @@ Changelog ~~~~~~~~~~~~~~~~ * Removed support for Python 2.7, Django 1.8 and Django 1.10 + * Added hidden field to the form builder (Ross Crawford-d'Heureuse) * Fix: Do not remove stopwords when generating slugs from non-ASCII titles, to avoid issues with incorrect word boundaries (Sævar Öfjörð Magnússon) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 77e19c3f91..7d92c9d6bf 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -257,6 +257,7 @@ Contributors * Mads Jensen * Samir Shah * Patrick Woods +* Ross Crawford-d'Heureuse Translators =========== diff --git a/docs/releases/2.0.rst b/docs/releases/2.0.rst index 3788b77446..e16faae443 100644 --- a/docs/releases/2.0.rst +++ b/docs/releases/2.0.rst @@ -13,6 +13,8 @@ What's new Other features ~~~~~~~~~~~~~~ + * Added hidden field to the form builder (Ross Crawford-d'Heureuse) + Bug fixes ~~~~~~~~~ diff --git a/wagtail/tests/testapp/migrations/0021_hidden_form_field.py b/wagtail/tests/testapp/migrations/0021_hidden_form_field.py new file mode 100644 index 0000000000..5f246fa481 --- /dev/null +++ b/wagtail/tests/testapp/migrations/0021_hidden_form_field.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.6 on 2017-10-13 16:10 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tests', '0020_customdocument'), + ] + + operations = [ + migrations.AlterField( + model_name='formfield', + name='field_type', + field=models.CharField(choices=[('singleline', 'Single line text'), ('multiline', 'Multi-line text'), ('email', 'Email'), ('number', 'Number'), ('url', 'URL'), ('checkbox', 'Checkbox'), ('checkboxes', 'Checkboxes'), ('dropdown', 'Drop down'), ('multiselect', 'Multiple select'), ('radio', 'Radio buttons'), ('date', 'Date'), ('datetime', 'Date/time'), ('hidden', 'Hidden field')], max_length=16, verbose_name='field type'), + ), + migrations.AlterField( + model_name='formfieldwithcustomsubmission', + name='field_type', + field=models.CharField(choices=[('singleline', 'Single line text'), ('multiline', 'Multi-line text'), ('email', 'Email'), ('number', 'Number'), ('url', 'URL'), ('checkbox', 'Checkbox'), ('checkboxes', 'Checkboxes'), ('dropdown', 'Drop down'), ('multiselect', 'Multiple select'), ('radio', 'Radio buttons'), ('date', 'Date'), ('datetime', 'Date/time'), ('hidden', 'Hidden field')], max_length=16, verbose_name='field type'), + ), + migrations.AlterField( + model_name='jadeformfield', + name='field_type', + field=models.CharField(choices=[('singleline', 'Single line text'), ('multiline', 'Multi-line text'), ('email', 'Email'), ('number', 'Number'), ('url', 'URL'), ('checkbox', 'Checkbox'), ('checkboxes', 'Checkboxes'), ('dropdown', 'Drop down'), ('multiselect', 'Multiple select'), ('radio', 'Radio buttons'), ('date', 'Date'), ('datetime', 'Date/time'), ('hidden', 'Hidden field')], max_length=16, verbose_name='field type'), + ), + ] diff --git a/wagtail/wagtailforms/forms.py b/wagtail/wagtailforms/forms.py index 8f130d565c..dc5b40ed2f 100644 --- a/wagtail/wagtailforms/forms.py +++ b/wagtail/wagtailforms/forms.py @@ -76,6 +76,9 @@ class FormBuilder(object): def create_checkbox_field(self, field, options): return django.forms.BooleanField(**options) + def create_hidden_field(self, field, options): + return django.forms.CharField(widget=django.forms.HiddenInput, **options) + FIELD_TYPES = { 'singleline': create_singleline_field, 'multiline': create_multiline_field, @@ -89,6 +92,7 @@ class FormBuilder(object): 'radio': create_radio_field, 'checkboxes': create_checkboxes_field, 'checkbox': create_checkbox_field, + 'hidden': create_hidden_field, } @property diff --git a/wagtail/wagtailforms/models.py b/wagtail/wagtailforms/models.py index 9165b131eb..b0c6d9bf87 100644 --- a/wagtail/wagtailforms/models.py +++ b/wagtail/wagtailforms/models.py @@ -33,6 +33,7 @@ FORM_FIELD_CHOICES = ( ('radio', _('Radio buttons')), ('date', _('Date')), ('datetime', _('Date/time')), + ('hidden', _('Hidden field')), ) diff --git a/wagtail/wagtailforms/tests/test_forms.py b/wagtail/wagtailforms/tests/test_forms.py index 30937d3699..13aad9b1b7 100644 --- a/wagtail/wagtailforms/tests/test_forms.py +++ b/wagtail/wagtailforms/tests/test_forms.py @@ -111,6 +111,13 @@ class TestFormBuilder(TestCase): field_type='checkbox', required=True, ) + FormField.objects.create( + page=self.form_page, + sort_order=1, + label="A Hidden Field", + field_type='hidden', + required=False, + ) # Create a form builder self.fb = FormBuilder(self.form_page.get_form_fields()) @@ -135,6 +142,7 @@ class TestFormBuilder(TestCase): self.assertIn('your-favourite-python-ide', field_names) self.assertIn('your-choices', field_names) self.assertIn('i-agree-to-the-terms-of-use', field_names) + self.assertIn('a-hidden-field', field_names) # All fields have proper type self.assertIsInstance(form_class.base_fields['your-name'], forms.CharField) @@ -149,8 +157,10 @@ class TestFormBuilder(TestCase): self.assertIsInstance(form_class.base_fields['your-favourite-python-ide'], forms.ChoiceField) self.assertIsInstance(form_class.base_fields['your-choices'], forms.MultipleChoiceField) self.assertIsInstance(form_class.base_fields['i-agree-to-the-terms-of-use'], forms.BooleanField) + self.assertIsInstance(form_class.base_fields['a-hidden-field'], forms.CharField) # Some fields have non-default widgets self.assertIsInstance(form_class.base_fields['your-message'].widget, forms.Textarea) self.assertIsInstance(form_class.base_fields['your-favourite-python-ide'].widget, forms.RadioSelect) self.assertIsInstance(form_class.base_fields['your-choices'].widget, forms.CheckboxSelectMultiple) + self.assertIsInstance(form_class.base_fields['a-hidden-field'].widget, forms.HiddenInput)