Reputation: 776
I'm using Timex.DateTime for working with datetimes through my project & so far it does the job.
However, I tried to make a custom builder for the Phoenix.HTML.Form for editing the datetime publish_date
following the documentation:
<div class="form-group">
<%= label f, :publish_date, class: "control-label" %>
<br />
<%= datetime_select f, :publish_date, class: "form-control", builder: fn b -> %>
Time: <%= b.(:hour, []) %> : <%= b.(:min, []) %> : <%= b.(:sec, []) %>
<br />
Date: <%= b.(:day, []) %> / <%= b.(:month, []) %> / <%= b.(:year, []) %>
<% end %>
<%= error_tag f, :publish_date %>
I understand from the output that I work with different types, but I don't know how to transform them, I'm still learning Phoenix, so if you have a better approach for this problem that'd be also useful.
StackTrace:
Request: HEAD /news/12/edit
** (exit) an exception was raised:
** (ArgumentError) unrecognized time #<DateTime(2021-12-31T23:59:00Z)>
(phoenix_html) lib/phoenix_html/form.ex:966: Phoenix.HTML.Form.time_value/1
(phoenix_html) lib/phoenix_html/form.ex:895: Phoenix.HTML.Form.datetime_select/3
(estrada) web/templates/news_article/form.html.eex:41: anonymous fn/2 in Estrada.NewsArticleView.form.html/1
(phoenix_html) lib/phoenix_html/form.ex:235: Phoenix.HTML.Form.form_for/4
(estrada) web/templates/news_article/form.html.eex:1: Estrada.NewsArticleView."form.html"/1
(estrada) web/templates/news_article/edit.html.eex:3: Estrada.NewsArticleView."edit.html"/1
(estrada) web/templates/layout/app.html.eex:48: Estrada.LayoutView."app.html"/1
(phoenix) lib/phoenix/view.ex:344: Phoenix.View.render_to_iodata/3
(phoenix) lib/phoenix/controller.ex:633: Phoenix.Controller.do_render/4
(estrada) web/controllers/news_article_controller.ex:1: Estrada.NewsArticleController.action/2
(estrada) web/controllers/news_article_controller.ex:1: Estrada.NewsArticleController.phoenix_controller_pipeline/2
(estrada) lib/phoenix/router.ex:261: Estrada.Router.dispatch/2
(estrada) web/router.ex:1: Estrada.Router.do_call/2
(estrada) lib/estrada/endpoint.ex:1: Estrada.Endpoint.phoenix_pipeline/1
(estrada) lib/plug/debugger.ex:93: Estrada.Endpoint."call (overridable 3)"/2
(estrada) lib/phoenix/endpoint/render_errors.ex:34: Estrada.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
EDIT: This form works well when I create a new post with it, it crashes only when I edit a post with it, i.e. it works when the form submits the data to the server, this happens only when I populate the datetime field.
Upvotes: 1
Views: 248
Reputation: 222188
The problem is that datetime_select
only supports a limited number of formats of input, and Timex.DateTime
does not match any of them.
You can manually convert the Timex.DateTime
into a supported format and pass that:
Replace
<%= datetime_select f, :publish_date, class: "form-control", builder: fn b -> %>
with
<%= datetime_select f, :publish_date, value: Timex.to_erlang_datetime(model.publish_date), class: "form-control", builder: fn b -> %>
(Remember to replace model
with your actual model.)
This workaround will hopefully not be needed in future versions of phoenix_html and timex when Elixir 1.3 is released with a builtin DateTime struct.
Upvotes: 2