Skip to content

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

@anichols-bto

Description

@anichols-bto

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions