alex1
Interval selector for form: how?
Hello everyone!
I have a question how to make an interval selector. I’m very new to elixir and phoenix and not sure how to solve my problem in the most idiomatic way. Here is what I’m trying to do and it doesn’t work:
<.simple_form id="paste-form" for={@form} phx-change="validate" phx-submit="save">
<.input type="select"
field={@form[:expiration]}
options={["One second", %Postgrex.Interval{secs: 1}]}
/>
...
</.simple_form>
If I do like this, I’ve got
protocol Enumerable not implemented for %{days: 0, microsecs: 0, months: 0, secs: 1} of type Postgrex.Interval (a struct)
So, how should I make an interval selector?
Marked As Solved
LostKobrakai
Any data you render to html will need to be converted to a string format, which can be placed in the final html. That’s what this protocol is used for. If it’s not implemented then it’s unknown how to convert the value you supplied to that corresponding string/html.
Again that’s how forms in html work. Their inputs have string values and string values are submitted (excluding file uploads). That’s what Ecto.Type.cast/1 callbacks for the ecto type of your field is meant to deal with. Convert from incoming weakly typed formats to stronger typed runtime values. If the ecto type you’re using doesn’t support the string format you need you can wrap it in a custom type you create, which can handle your needed string format and delegate to the underlying ecto type everywhere else.
Also Liked
tomciopp
You’re going to want to use iso8601 duration strings to convert to and from Postgrex.Interval
So in your html the value for 1 second would be P1S, etc.
Hermanverschooten
You cannot just put complex types into <option>, put values like 1,30,60,300 in there and convert them to the correct interval on the backend. Remember that all value-parameters in html are strings.
Last Post!
tomciopp
You’re going to want to use iso8601 duration strings to convert to and from Postgrex.Interval
So in your html the value for 1 second would be P1S, etc.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









