From dd9cd7aa0c0ab47db003adeceb58d77a8230dc71 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Thu, 21 Feb 2019 17:01:12 +0100 Subject: [PATCH] automatically fill slug field while typing --- .gitignore | 2 +- passbook/core/forms/applications.py | 3 ++- passbook/core/static/js/passbook.js | 18 ++++++++++++++++++ passbook/core/templates/base/skeleton.html | 1 + 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 passbook/core/static/js/passbook.js diff --git a/.gitignore b/.gitignore index aa12fbfe1..e26c51d5c 100644 --- a/.gitignore +++ b/.gitignore @@ -189,5 +189,5 @@ pyvenv.cfg pip-selfcheck.json # End of https://www.gitignore.io/api/python,django -static/ +/static/ local.env.yml diff --git a/passbook/core/forms/applications.py b/passbook/core/forms/applications.py index b4a105780..47693fea5 100644 --- a/passbook/core/forms/applications.py +++ b/passbook/core/forms/applications.py @@ -8,7 +8,8 @@ from passbook.core.models import Application, Provider class ApplicationForm(forms.ModelForm): """Application Form""" - provider = forms.ModelChoiceField(queryset=Provider.objects.all().select_subclasses()) + provider = forms.ModelChoiceField(queryset=Provider.objects.all().select_subclasses(), + required=False) class Meta: diff --git a/passbook/core/static/js/passbook.js b/passbook/core/static/js/passbook.js new file mode 100644 index 000000000..a52cfe21e --- /dev/null +++ b/passbook/core/static/js/passbook.js @@ -0,0 +1,18 @@ +function convertToSlug(Text) { + return Text + .toLowerCase() + .replace(/[^\w ]+/g, '') + .replace(/ +/g, '-') + ; +} + + +const $source = $('input[name=name]'); +const $result = $('input[name=slug]'); + +const typeHandler = function (e) { + $result.val(convertToSlug(e.target.value)); +} + +$source.on('input', typeHandler) // register for oninput +$source.on('propertychange', typeHandler) // for IE8 diff --git a/passbook/core/templates/base/skeleton.html b/passbook/core/templates/base/skeleton.html index de44486bb..5334d5a0c 100644 --- a/passbook/core/templates/base/skeleton.html +++ b/passbook/core/templates/base/skeleton.html @@ -24,6 +24,7 @@ + {% block scripts %} {% endblock %}