Skip to content

Commit c4d9778

Browse files
committed
add autocomplete demo
1 parent 3385e9b commit c4d9778

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

app/demos/forms.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
SubmitField,
3030
TextAreaField,
3131
)
32-
from wtforms.validators import Email, EqualTo, InputRequired, Length, Optional, Regexp
32+
from wtforms.validators import Email, EqualTo, InputRequired, Length, Optional, Regexp, ValidationError
3333

3434
from app.demos.custom_validators import RequiredIf
3535

@@ -313,7 +313,9 @@ class ConditionalRevealForm(FlaskForm):
313313
# This will mark this field as required if the how_prefer_contacted is set to email
314314
validators=[
315315
RequiredIf(
316-
"contact", "email", message="Enter an email address in the correct format, like name@example.com"
316+
"contact",
317+
"email",
318+
message="Enter an email address in the correct format, like name@example.com",
317319
)
318320
],
319321
)
@@ -322,7 +324,11 @@ class ConditionalRevealForm(FlaskForm):
322324
"Phone number",
323325
widget=GovTextInput(),
324326
validators=[
325-
RequiredIf("contact", "phone", message="Enter a telephone number, like 01632 960 001 or +44 808 157 0192")
327+
RequiredIf(
328+
"contact",
329+
"phone",
330+
message="Enter a telephone number, like 01632 960 001 or +44 808 157 0192",
331+
)
326332
],
327333
)
328334

@@ -339,3 +345,32 @@ class ConditionalRevealForm(FlaskForm):
339345
)
340346

341347
submit = SubmitField("Continue", widget=GovSubmitInput())
348+
349+
350+
class AutocompleteForm(FlaskForm):
351+
# Manually added list here, but could be dynamically assigned in server route
352+
countries = [
353+
"Canada",
354+
"China",
355+
"France",
356+
"Germany",
357+
"India",
358+
"Italy",
359+
"Japan",
360+
"South Korea",
361+
"United Kingdom",
362+
"United States",
363+
]
364+
365+
country = StringField(
366+
"Country",
367+
widget=GovTextInput(),
368+
validators=[InputRequired(message="Enter a country")],
369+
description="Start typing and select a suggestion",
370+
)
371+
372+
submit = SubmitField("Continue", widget=GovSubmitInput())
373+
374+
def validate_country(self, country):
375+
if country.data.title() not in self.countries:
376+
raise ValidationError(f"{country.data} is not a valid country")

app/demos/routes.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from werkzeug.exceptions import NotFound
66

77
from app.demos import bp
8-
from app.demos.forms import BankDetailsForm, ConditionalRevealForm, CreateAccountForm, KitchenSinkForm
8+
from app.demos.forms import AutocompleteForm, BankDetailsForm, ConditionalRevealForm, CreateAccountForm, KitchenSinkForm
99

1010

1111
@bp.route("/components", methods=["GET"])
@@ -66,3 +66,12 @@ def conditional_reveal():
6666
flash("Demo form successfully submitted", "success")
6767
return redirect(url_for("demos.forms"))
6868
return render_template("conditional_reveal.html", form=form)
69+
70+
71+
@bp.route("/forms/autocomplete", methods=["GET", "POST"])
72+
def autocomplete():
73+
form = AutocompleteForm()
74+
if form.validate_on_submit():
75+
flash("Demo form successfully submitted", "success")
76+
return redirect(url_for("demos.forms"))
77+
return render_template("autocomplete.html", form=form)

app/templates/demos/autocomplete.html

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{% extends "base.html" %}
2+
3+
{%- from 'govuk_frontend_jinja/components/breadcrumbs/macro.html' import govukBreadcrumbs -%}
4+
5+
{% block pageTitle %}{%- if form.errors %}Error: {% endif -%}Autocomplete – GOV.UK{% endblock %}
6+
7+
{% block beforeContent %}
8+
{{ super() }}
9+
{{ govukBreadcrumbs({
10+
'collapseOnMobile': True,
11+
'items': [
12+
{
13+
'text': "Home",
14+
'href': url_for('main.index')
15+
},
16+
{
17+
'text': "Forms",
18+
'href': url_for('demos.forms')
19+
},
20+
{
21+
'text': "Autocomplete"
22+
}
23+
]
24+
}) }}
25+
{% endblock %}
26+
27+
{% block content %}
28+
<div class="govuk-grid-row">
29+
<div class="govuk-grid-column-two-thirds">
30+
{{ super() }}
31+
<span class="govuk-caption-xl">Demo form</span>
32+
<h1 class="govuk-heading-xl">Autocomplete</h1>
33+
34+
<form action="" method="post" novalidate>
35+
{{ form.csrf_token }}
36+
37+
{{ form.country(params={
38+
'spellcheck': true,
39+
'attributes': {
40+
'list': "countries"
41+
},
42+
}) }}
43+
44+
<datalist id="countries">
45+
{% for country in form.countries %}
46+
<option value="{{country}}">
47+
{% endfor %}
48+
</datalist>
49+
50+
{{ form.submit }}
51+
</form>
52+
</div>
53+
</div>
54+
{% endblock %}

app/templates/demos/forms.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<span class="govuk-caption-xl">Demo</span>
2727
<h1 class="govuk-heading-l">Forms</h1>
2828
<ul class="govuk-list govuk-list--bullet">
29+
<li><a class="govuk-link" href="{{ url_for('demos.autocomplete') }}">Autocomplete</a></li>
2930
<li><a class="govuk-link" href="{{ url_for('demos.bank_details') }}">Bank details</a></li>
3031
<li><a class="govuk-link" href="{{ url_for('demos.conditional_reveal') }}">Conditional reveal</a></li>
3132
<li><a class="govuk-link" href="{{ url_for('demos.create_account') }}">Create account</a></li>

0 commit comments

Comments
 (0)