Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid state on Contact not reverted after email validation handler #20

Open
anichols-bto opened this issue Apr 2, 2024 · 0 comments

Comments

@anichols-bto
Copy link

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:

Apr-02-2024.09-29-15.mp4

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:

@app.route('/contacts/<contact_id>/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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant