Skip to content

Invalid CSRF token + missing errors #67

@abitdodgy

Description

@abitdodgy

I'm using drab to fetch a URL and prepopulate a form. However, when I submit the form (vanilla, not using drab for the submission), I get an invalid csrf token. I'm assuming it's because the prefetch request that I did through drab used up the csrf token? What would be the best way to go about refreshing it?

Here's what I'm doing:

<%= form_for @changeset, @action, [novalidate: true], fn f -> %>
  <%= FooWeb.InputHelpers.input f, :url, drab_keyup: "scrape_url", drab_options: "debounce(500)" %>
...

In my commander:

  def scrape_url(socket, %{params: %{"link" => link_params}}) do
    with %Ecto.Changeset{valid?: true, changes: %{url: url}} <- Media.change_link_url(%Media.Link{}, link_params),
      {:ok, %{status_code: 200} = data} <- FurlexHelper.unfurl(url)
    do
      changeset = Media.change_link(%Media.Link{}, Map.put(link_params, "metadata", data))
      Drab.Live.poke socket, "form.html", changeset: changeset
    else
      %Ecto.Changeset{valid?: false} = changeset ->
        Drab.Live.poke socket, "form.html", changeset: changeset
      :error ->
        nil
    end
  end

Another issue I'm running into is not being able to show changeset errors. For example, the else branch above returns an invalid changeset with errors which the form should render, but the form doesn't render out the errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions