rottenfishbone

rottenfishbone

Is it typical behaviour for multiple phx-value-* to require strings?

Using:

defp deps do
[
#...
{:phoenix, "~> 1.7.7"},
{:phoenix_html, "~> 3.3"},
{:phoenix_live_view, "~> 0.20.1"},
#...
]

When defining a function component that has multiple phx-value-* defined using raw elixir types (e.g. phx-value-id={@element.id}, phx-value-name={@element.name}), only a single value is passed to the handle_event params.

However, if I use raw strings or even string templates(?) (e.g. phx-value-id={"#{@element.id}"} this is not an issue.

Here is my case:

#...
<.button 
          class="btn-error"
          phx-click="toggle-disabled"
          phx-value-id={@element.id}}
          phx-value-disabled={@element.disabled}
          data-confirm="Are you sure?">
    Toggle
</.button>
#...

Passes %{"id" => "1", "value"=>""} to params

Whereas:

#...
<.button 
          class="btn-error"
          phx-click="toggle-disabled"
          phx-value-id={"#{@element.id}"}
          phx-value-disabled={"#{@element.disabled}"}
          data-confirm="Are you sure?">
    Toggle
</.button>
#...

Passes the data as expected. This also works if I only use string literals. phx-value is affected by this as well and stays empty if I define a phx-value-* using an elixir type(? not sure if this is the root cause).

This is defined inside a function component, not a live component. Not sure if that changes this either.

While I have solved my issue by using templates, I’m not sure if this is a bug that should be reported, or expected behaviour.

Marked As Solved

sodapopcan

sodapopcan

In short, yes.

HTML works only with strings, so any values you put in the DOM must be converted to a string. The behaviour you’re seeing is likely because setting an attribute to something falsey—ie, nil or false—will cause it to not render at all. While I don’t have an example off the top of my head, this is a convenience feature that I make use of quite often! Otherwise, it’s best to make sure your phx-values are explicitly strings if you think they could be fasley.

And welcome to the forums!

Also Liked

rottenfishbone

rottenfishbone

That makes sense and I have indeed noticed I get an error about HTML safe when I try to use a type such as a list.

I see now I misunderstood what was happening here with my issues here. I noticed integers would be converted to strings which had me incorrectly assuming a boolean would, as well.

Thanks for the quick reply, and the conditional rendering is actually something I might start using as well.

Where Next?

Popular in Questions Top

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement