Skip to content

How to deal with "invalid CSRF token" when re rendering the form with Drab

Tomek Gryszkiewicz edited this page Feb 15, 2018 · 1 revision

If you re-render the whole form which uses CSRF token (like with form_for helper), you may experience "invalid CSRF token" on submit this form. The solution is to store the token before rendering the form, and put it back again:

# safe the existing token
{:ok, %{"value" => token}} = query_one socket, "form > input[name='_csrf_token']", :value
# re-render the form
poke socket, "#form_holder", innerHTML: Phoenix.View.render(MyView, "form.html", assign: "value")
# set the token back
set_attr socket, "form > input[name='_csrf_token']", value: token
Clone this wiki locally