Skip to content

Commit d18f82a

Browse files
committed
update chapter with the failing validation FT
1 parent d10b459 commit d18f82a

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

chapter_15_advanced_forms.asciidoc

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ NOTE: Personal opinion here: I could have used `super`, but I prefer not to use
774774
Anything else is too error-prone, and I find it ugly besides. YMMV.
775775

776776

777-
And we're there! All the unit tests pass:
777+
Let's run the tests! All the unit tests pass:
778778

779779
[subs="specialcharacters,macros"]
780780
----
@@ -785,7 +785,67 @@ Ran 30 tests in 0.082s
785785
OK
786786
----
787787

788-
And so does our FT for validation:
788+
But we still have something to do about our FTs:
789+
[subs="specialcharacters,macros"]
790+
----
791+
$ pass:quotes[*python manage.py test functional_tests.test_list_item_validation*]
792+
[...]
793+
FAIL: test_cannot_add_duplicate_items (functional_tests.test_list_item_validation.ItemValidationTest.test_cannot_add_duplicate_items)
794+
----------------------------------------------------------------------
795+
[...]
796+
AssertionError: '' != "You've already got this in your list"
797+
+ You've already got this in your list
798+
----
799+
800+
The error message isn't being displayed because we are not using the Bootstrap
801+
classes. Although it would have been nice to minimise hand-written html and
802+
utilise Django instead, it seems like we need to bring back our custom
803+
`<input>` and add a few attributes manually:
804+
805+
[role="sourcecode"]
806+
.src/lists/templates/base.html
807+
====
808+
[source,diff]
809+
----
810+
@@ -13,13 -14,26 +14,26 @@@
811+
<div class="col-lg-6 text-center">
812+
<h1 class="display-1 mb-4">{% block header_text %}{% endblock %}</h1>
813+
- <form method="POST" action="{% block form_action %}{% endblock %}">
814+
- {{ form.text }}
815+
+ <form
816+
+ class="was-validated" <1>
817+
+ method="POST"
818+
+ action="{% block form_action %}{% endblock %}"
819+
+ >
820+
{% csrf_token %}
821+
+ <input
822+
+ id="id_text"
823+
+ name="text"
824+
+ class="form-control form-control-lg {% if form.errors %}is-invalid{% endif %}" <2>
825+
+ placeholder="Enter a to-do item"
826+
+ value="{{ form.text.value }}"
827+
+ aria-describedby="id_text_feedback"
828+
+ required
829+
+ />
830+
{% if form.errors %}
831+
- <div class="invalid-feedback">{{ form.errors.text }}</div>
832+
+ <div class="invalid-feedback">{{ form.errors.text.0 }}</div> <3>
833+
{% endif %}
834+
</form>
835+
</div>
836+
----
837+
====
838+
839+
<1> We give `.was-validated` to the `<form>`
840+
<2> We provide an `<input>` and the most important custom setting will be its
841+
`class`. As you can see, we can use conditionals even for providing
842+
additional `class` -es.
843+
<3> If you try to just simply display `form.errors.text` you'll see it ends up
844+
as a list item in an unordered list. To avoid that, let's just
845+
select `form.errors.text.0`.
846+
847+
848+
Now when we run the FT for validation again:
789849
790850
[subs="specialcharacters,macros"]
791851
----
@@ -815,9 +875,6 @@ Hooray! Time for a final commit, and a wrap-up of what we've learned about
815875
testing views over the last few chapters.((("", startref="DITlist15")))
816876
817877
818-
819-
820-
821878
=== Wrapping Up: What We've Learned About Testing Django
822879
823880
((("class-based generic views (CBGVs)", "key tests and assertions")))((("Django framework", "class-based generic views")))We're

0 commit comments

Comments
 (0)