saveman71

saveman71

Why does <.form> discard method and CSRF token when :action is absent, and is action="" a reliable way to submit to the current URL?

Hello !

We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id), so that changeset error re-renders keep the browser URL stable: F5 reloads the form instead of GET-ing the index/show URL, and our URL matching JS bundle loading keeps working without hacks.

This has been a recurring source of issues on our end so we’re currently writing an internal guide/credo check to enforce that.

Regarding the form target, in plain HTML you’d get this for free by omitting action: the form then submits to the current URL.

But Phoenix.Component.form/1 explicitly discards method and the CSRF token in that case (docs).

Today we pass the page’s own URL explicitly:

<.form for={@form} action={~p"/things/#{@thing}/edit"} method="put">

That works, but means that a template shared between new and edit has to conditionally rebuild the URL the browser is already on, adding extra steps to check and maintain.

Two questions:

  1. Is there a reason for discarding method/csrf_token when :action is absent for dead views?
  2. action="" seems to work as a “submit to current URL”: HTML treats an empty action as the document’s URL, and it’s truthy so the component renders method and the CSRF token. Is that supported behavior we can rely on?

A side effect is that it’s going to call Plug.CSRFProtection.get_csrf_token_for(""), which returns a valid token but unscoped as far as I understand. Is this bad?

Thanks in advance for any insight.

Most Liked

LostKobrakai

LostKobrakai

Not just in this case. Only false and nil are falsy in elixir, everything else is truthy.

nseaSeb

nseaSeb

<.form for={@form} action="" method={if @thing, do: "put", else: "post"}>
It guess in this case “” is truthy in Elixir, so <.form> takes the “action provided” branch and normally generates the hidden _method field and the CSRF token.
Maybe send the URL by another approach ?

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42158 114
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement