Skip to content

Conversation

@ryanwinchester
Copy link
Contributor

@ryanwinchester ryanwinchester commented Sep 19, 2025

Issue

If I try to use a select with struct values, I get an error because of an is_map check, and assumes that it is an option group.

We should be able to pass as values any struct that implements the Phoenix.HTML.Safe protocol.

Changes

  • added a struct check
  • added a new test assertion that fails without this fix:
  1) test options_for_select/2 simple (Phoenix.HTML.FormTest)
     test/phoenix_html/form_test.exs:262
     ** (Protocol.UndefinedError) protocol Enumerable not implemented for type DateTime (a struct).
     This protocol is implemented for the following type(s): Date.Range, File.Stream, Function,
     GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream

     Got value:

         ~U[2025-01-01 06:30:00.000000Z]

     code: assert options_for_select([
        {"Open", ~U[2025-01-01 06:30:00.000000Z]},
        {"Close", ~U[2025-01-01 18:30:00.000000Z]}
      ], [
     stacktrace:
       (elixir 1.18.4) lib/enum.ex:1: Enumerable.impl_for!/1
       (elixir 1.18.4) lib/enum.ex:166: Enumerable.reduce/3
       (elixir 1.18.4) lib/enum.ex:4515: Enum.reduce/3
       (phoenix_html 4.2.1) lib/phoenix_html/form.ex:395: Phoenix.HTML.Form.option/4
       (phoenix_html 4.2.1) lib/phoenix_html/form.ex:360: anonymous fn/4 in 
        Phoenix.HTML.Form.escaped_options_for_select/3
       (elixir 1.18.4) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
       (phoenix_html 4.2.1) lib/phoenix_html/form.ex:347: Phoenix.HTML.Form.options_for_select/3
       test/phoenix_html/form_test.exs:306: (test)

@ryanwinchester ryanwinchester changed the title Allow datetime as form option values Allow structs as select option values Sep 19, 2025
@josevalim
Copy link
Member

@ryanwinchester oops, can you please run the formatter? 😬

@josevalim josevalim merged commit 671042c into phoenixframework:main Sep 21, 2025
2 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants