bigskysoftware / bigskysoftware/contact-app
Invalid state on Contact not reverted after email validation handler
- Dominant language
- JavaScript
- Stars
- 130
- Forks
- 59
- PR merge metrics
- No merged PRs in 30d
Description
In the chapter on using htmx to send the email to the server to surface validation errors prior to form submission:
the email of the contact gets set on the `Contact.db` and does not get revered to the original state. The effect is that if the user navigates back to the list view, they will see duplicated emails. See video:
https://github.com/bigskysoftware/contact-app/assets/59518632/9f0460a1-2072-44ce-bb7e-f45ab161bf47
I thought maybe I messed up the htmx and was calling a POST or PATCH somewhere unexpectedly. Instead it was just mutating the dummy `db`. I was able to fix with this updated handler:
```python
@app.route('/contacts//email', methods=['GET'])
def contacts_email_get(contact_id: int):
contact = Contact.find(contact_id)
# Temporarily store original email
old_email = contact.email
contact.email = request.args.get('email')
contact.validate()
# Restore email to previous value
contact.email = old_email
return contact.errors.get('email') or ''
```
P.S. Thanks for the book!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by locating the contacts_email_get handler for the GET /contacts//email route and inspect how it validates the requested email against the dummy Contact.db. Reproduce the email-validation flow, then verify that returning to the list view does not show duplicate emails and that validation errors are still surfaced.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flask, python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100