alex1

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

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

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

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

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.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement