zac
Basic question here (I’m just starting with LiveView and relative Phoenix newbie, too). ![]()
I’d like to build a page that dynamically reloads context when the user chooses a new option from a select list.
Functionally, like having a menu of links on the left, picking one, and the page redraws. But, instead of a long list of items, just a select input.
My problem is getting the a handle_event() on change of the select. (Key point, I don’t want the user to pick an item and them hit a "submit” button… idea is they just pick a new item in the popup list, and the page dynamically changes).
Here’s one (of many) variations I’ve tried, the problem with this one is that it doesn’t send the team ID to handle_event() (I realize this is a bit of a mess right, but… just my latest experiment):
<.form id="navigation" action={~p"/log"} method="post">
<select :if={@teams} class="select select-info" phx-change="navigate">
<option disabled selected>Pick a team</option>
<option :for={team <- @teams} value={team.id} phx-value-id={team.id}>
{team.name}
</option>
</select>
</.form>
Obviously, it’s for picking different teams… them the page updates, with various team details.
The above doesn’t work because I don’t get the value I need, team.id, instead I get this in the handler:
[debug] HANDLE EVENT "navigate" in WasteWalkWeb.LogLive
Parameters: %{"_target" => ["undefined"]}
What I’m shooting for is a simple handle_event("navigate", %{"id" => id}, socket) function.
Doing this with a simple list of value works, but trying to move it into a select has me stumped. Thanks.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
krishandley
Try adding a name attribute to the select.
kevinschweikert
You would add a
phx-validatebinding as described here: Form bindings — Phoenix LiveView v1.2.5Then in your live view module you add:
If you don’t need the form backed by a schema/changeset you can just define a map with the fields and declate you
formassign like this ( Phoenix.Component — Phoenix LiveView v1.2.5 )So in the end your form component looks like this:
zac
Thank you… that was it!
zac
Thank you, that was helpful. I changed things up using a “validate” call.
I still feel like the “final” implementation here is a bit odd looking:
I’m still figuring out the core components.
Is it odd that I’m mixing
<.form/>and native HTML<select>elements? I had expected something more like this, but I’m not clear on how this would work withselectandoption…LostKobrakai
zac
Thanks @LostKobrakai, very helpful. Your version is responsive (for some reason, using
phx-validatedoesn’t seem to work… thought it would?), but minor tweak gets us there: ended up tweaking syntax onfield.Here’s the final impl (also renamed “team_select” to “team_form” for clarity):
kevinschweikert
Sorry for the confusion, that’s on me! The is no
phx-validate! i just got it mixed up because i always writephx-change="validate".