augnustin

augnustin

Hello,

I’d like my button("content", to: url) to have extra hidden fields in it, like button("content", to: url, params: %{field: "value"}) is this possible?

The same question went in Rails: https://stackoverflow.com/questions/4886963/how-to-add-additional-params-to-a-button-to-form and it is now possible. :smile:

Cheers

Showing Posts 1 to 10

albydarned

albydarned

From the docs

button("hello", to: "/world", method: "get", class: "btn")
#=> <button class="btn" data-method="get" data-to="/world">hello</button>

Try

button("hello", to: "/world", method: "get", class: "btn", custom: "property")
#=> <button class="btn" data-method="get" data-to="/world" custom="property">hello</button>

Image from my test:
image

augnustin

augnustin OP

Apologies if I have been unclear but I’d like custom=property to be sent out within the ajax call that submits the form, while your solution only adds an html custom attribute, doesn’t it?

albydarned

albydarned

I see, so this button is in a form?

Have you considered adding properties as hidden inputs?

LostKobrakai

LostKobrakai

I’m not sure why this was added to rails, but conceptionally it seems quite strange to exist. GET parameters are part of the url, so I’m not sure why they should be added via options on the button. Phoenixs’ route helpers allow you to add GET parameters to urls as well as the URI module of the elixir core. If you’re looking for additional body params you’d need to wrap the button in a form to submit, which has some hidden inputs.

OvermindDL1

OvermindDL1

Phoenix doesn’t use javascript to perform ajax calls, Phoenix is just a server that lets you do on the front-end whatever you wish. If you have something that needs to perform ajax calls then it is that which should do it, phoenix will just handle the back-end side. What front-end are you using that performs ajax in the browser?

augnustin

augnustin OP

Completely agreed with the fact that GET parameters should be in the query URL.

But button\2 is actually more useful in the case of a POST/PUT/DELETE query IMHO, where params act as extra hidden fields.

Anyway, since it is phoenix_html.js which actually submits data, it could be conditional on the method so that GET is in the query parameter, and others are hidden fields.

I guess that’s what Rails does.

Cool thing about this is that method can be dynamic without having to rewrite the logic:

= button("content", to: "/example", method: method, params: params)
/ instead of
= if method == :get do
  = button("content", to: "/example?#{params |> Enum.map(fn k, v -> "#{k}=#{v}" end) |> Enum.join("&")}"
- else
  = button("content", to: "/example", method: method, params: params)
augnustin

augnustin OP

Oh ok, I thought it would, like rails_ujs does. But apparently it does has JS dependency.

Of course, one can do without it, but in this case, it seems pretty reasonable to use it the phoenix-way so that front-end and back-end plug each other easily.

Rails is really great at doing this, and I guess if phoenix is so much inspired from it, it means it is trying to reach the same convenience.

augnustin

augnustin OP

Basically instead of

      content_tag(:button, text, [data: [method: method, to: to] ++ csrf_data] ++ opts)

I’d expect to go for:

      content_tag(:form, text, method: method, action: to) do
        if method != :get do
          hidden_field(:csrf_token, Plug.CSRFProtection.get_csrf_token())
        end
        Enum.map(opts[:params], fn k, v -> 
          hidden_field(k, v)
        end) 
        content_tag(:button, text, type: :submit)
      end

And this would work for either :get and :post.

LostKobrakai

LostKobrakai

Phoenix’s button handling with method is mostly a convenience to not need to wrap just a button with a form, to defines nothing more than a url/method. If you need more data as part of the request use a form with hidden fields. You’re imho past the point there the convenience does make much sense.
For get requests you could alter the url, but a form will also just use get params if you submit it with the GET method. I guess both ways are viable for get parameters.

If you’d use form_for instead of content_tag(:form, …) you’d get the csrf token and the method override automatically and it would therefore work for any http method.

OvermindDL1

OvermindDL1

No clue what rails_ujs is, but I can’t imagine such a feature working without javascript support for it, and thus it will fail when javascript is disabled. It’s really not something that should be relied on.

Where Next? Top

Trending in Questions Top

axelson
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
yasoob
Hi all! I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
PJUllrich
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews