neurodynamic
I’m trying to make a radio button selector for a public/private setting in a Phoenix 1.7 app using LiveView, and I feel like I’m missing something probably really obvious.
Schema:
field(:visibility, Ecto.Enum, values: [:public, :private], default: :private)
Relevant form .heex code:
<fieldset>
<.input field={@form[:visibility]}
id="visibility_public"
type="radio"
label="Public"
value="public"
/>
<.input field={@form[:visibility]}
id="visibility_private"
type="radio"
label="Private"
value="private"
/>
</fieldset>
At the moment, nothing takes when I click on either button. I think I probably need to set the checked attribute somehow but I haven’t been able to work out the right way to do that.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
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 8 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
neurodynamic
Already done, but they went with making the exclusion of support explicit. Conversation is here: Adds radio input case to `input` function definition in `CoreComponents` by neurodynamic · Pull Request #5506 · phoenixframework/phoenix · GitHub
petrus-jvrensburg
Why not put these in a PR to Phoenix? Would be great to have it in core components by default.
neurodynamic
I also didn’t know about
Ecto.Enum.dump_valuesbefore seeing this; that’s a great helper functionneurodynamic
omg thank you both of you; this would’ve taken me forever to figure out!
For people looking at this in the future, @codeanpeace’s commit with the fix for the radio input in
CoreComponentsis here, and that change needs to be made before the changes in this commit will completely work.codeanpeace
After some digging, I got it working after realizing the
checkedattribute isn’t rendered by the default genericinputfunction component and needs to be explicitly set which is what the default checkboxinputfunction component does.https://github.com/phoenixframework/phoenix/blob/3c5bae721a965e91c0fe61c79a6f4d78fbf3b22f/installer/templates/phx_web/components/core_components.ex#L312-L325
You can either update the generic catchall
inputfunction to render thecheckedattribute or preferably add a radioinputfunction component that renders it.While that did properly check the other radio buttons upon first click, I noticed that clicking back to the original radio button did not properly check it upon first click. This seems to be because the
phx-changeform validation handler compares the form params to the original resource assigned to the socket and doesn’t register that change in its response to the client.I ended up working around this by basing the value of the
checkedattribute off of both the form field and form params. It needs to be both because the form params are originally empty.https://github.com/codeanpeace/live_cal/commit/736b4e307391b68cd600446f30a112f9afeacab8
To clean it up a bit, I added a
radio_fieldsetfunction component that generates the necessary radio button inputs based off of theEcto.Enumfield values.https://github.com/codeanpeace/live_cal/commit/11d246d11454a7a8e943685444e0b3d4cb9d3649
sodapopcan
Ah ya, for the default you would need to also check for
nilto set a default (or assign a default in the schema). So likechecked={is_nil(@form[:visibility]) or @form[:visibility] == "public"},checked={@form[:visibility] == "private"}. But ya, I just went through all of this yesterday and figured it would be fresh in mind to help but apparently it’s not, lol. I had the same “double click” problem as well though can’t explain it! Just once I had all the proper checks things started working. I did also see, though, that sometimes thevaluewould be set toon. I’ll be looking at it again soon and can maybe report back if no one else has chimed in by then.neurodynamic
That doesn’t seem to have an impact as far as I can tell. With or without that, it doesn’t set the default value as I’d expect, and it also seems to take two clicks to effectively select one of the buttons. I thought maybe it was an issue with needing to store the data as a raw assigns value, so I also tried this
with this
but that doesn’t seem to help either
sodapopcan
You could make a more succinct one but yes, you need to set
checked.Something like:
checked={@form[:visibility].value == "public"}andchecked={@form[:visibility].value == "private"}. You could add a function head inCoreComponentsfortype: "radio"and abstract away some of that stuff, but this is the quick solution.