#Problems with echo redirect and htmx

3 messages · Page 1 of 1 (latest)

dire jewel
#

Basically I want to be able using echo to return from a post request either an error that then gets swapped into the hx-target on error or when no error occurs redirect to the user page. However this doesn't work as the content of the user page just gets rendered inside the hx-target paragraph. Maybe somebody has a workaround or better idea on how to implement this than I do.

I have a handler function similar to this that is mapped to post /auth/register

func (h AuthHandler) HandleRegister(c echo.Context) error {
    // Perform the registration

    if err != nil {
        return err
    }

    return c.Redirect(http.StatusMovedPermanently, "/user")
}

this is called here:

<form hx-post="/auth/register" hx-trigger="submit" hx-target="#error" class="">
  <label for="email">Email</label>
  <input type="email" name="email">
  <label for="password">Password</label>
  <input type="password" name="password">
  <label for="confirm-password">Confirm Password</label>
  <input type="password" name="confirm-password">
  <p id="error"></p>
  <button type="submit">Register</button>
</form>
meager wyvern
#

Save yourself a lot of trouble and use:
https://htmx.org/extensions/response-targets/

(imo this should be part of htmx core rather than an extension)

heavy drum
#

You'll need to send a 'HX-Redirect' header rather than using the Echo Context.Redirect method:

https://htmx.org/reference/#response_headers