Repository: bookwyrm-social/bookwyrm

Issue:
When validating the form for adding book manually, some fields are emptied if a field is not valid
**Describe the bug**
When adding manually an item (book), if a field is not valid, after hitting enter some fields are emptied (values removed), such as the author, the subject, the cover, etc.

Steps to reproduce the behavior:
1. Add manually a book.
2. Fill the form with a publication date having only the year.
3. Add an author, subjects and a cover.
4. Save the form.
5. See the emptied fields.

**Expected behavior**
All filled in fields should keep their value after validating the form.

**Instance**
https://bw.diaspodon.fr

**Additional context**
Add any other context about the problem here.

---

**Desktop (please complete the following information):**
    - OS: Archlinux
    - Browser: Firefox
    - Version: 108



Discussion:
For the cover file select field this is related to #2421

I'm guessing for the other fields the fix should hopefully be fairly straightforward
Did a bit more manual testing on this:

When form submit returns with a validation error (i.e. only selecting a publishing year, not month/day):
* Subjects field
  * Adding two subjects in the initial create form
  * on the form with validation errors: Only one subject field visible and with an empty value 
* Author field
  * Adding two authors in the initial create form
  * on the form with validation errors: Only one author field visible and with an empty value
* Load cover from url field (fix in #2572)
  * Paste image url in this field on the initial create form
  * on the form with validation errors: value is empty

When form submit ends up on the "Confirmation" screen:
* Subjects field
  * Adding two subjects in the initial create form
  * On the confirmation screen there is only one subject field with an empty value
  * When going back from the confirmation screen to the create form only the first subject appears again
  * When confirming, no subjects are stored

Resolving patch:
diff --git a/bookwyrm/views/books/edit_book.py b/bookwyrm/views/books/edit_book.py
--- a/bookwyrm/views/books/edit_book.py
+++ b/bookwyrm/views/books/edit_book.py
@@ -45,6 +45,7 @@ def post(self, request, book_id):
         data = {"book": book, "form": form}
         ensure_transient_values_persist(request, data)
         if not form.is_valid():
+            ensure_transient_values_persist(request, data, add_author=True)
             return TemplateResponse(request, "book/edit/edit_book.html", data)
 
         data = add_authors(request, data)
@@ -102,11 +103,13 @@ def post(self, request):
                 "authors": authors,
             }
 
-        ensure_transient_values_persist(request, data)
-
         if not form.is_valid():
+            ensure_transient_values_persist(request, data, form=form)
             return TemplateResponse(request, "book/edit/edit_book.html", data)
 
+        # we have to call this twice because it requires form.cleaned_data
+        # which only exists after we validate the form
+        ensure_transient_values_persist(request, data, form=form)
         data = add_authors(request, data)
 
         # check if this is an edition of an existing work
@@ -139,9 +142,15 @@ def post(self, request):
         return redirect(f"/book/{book.id}")
 
 
-def ensure_transient_values_persist(request, data):
+def ensure_transient_values_persist(request, data, **kwargs):
     """ensure that values of transient form fields persist when re-rendering the form"""
     data["cover_url"] = request.POST.get("cover-url")
+    if kwargs and kwargs.get("form"):
+        data["book"] = data.get("book") or {}
+        data["book"]["subjects"] = kwargs["form"].cleaned_data["subjects"]
+        data["add_author"] = request.POST.getlist("add_author")
+    elif kwargs and kwargs.get("add_author") is True:
+        data["add_author"] = request.POST.getlist("add_author")
 
 
 def add_authors(request, data):


Test patch: