From 5ce67bf692c26f46290809886202f5fed3c68600 Mon Sep 17 00:00:00 2001 From: Benjamin Milde Date: Wed, 22 Jan 2025 18:18:54 +0100 Subject: [PATCH] Allow keyword list options to use nil as key and/or value --- lib/phoenix_html/form.ex | 13 +++++++------ test/phoenix_html/form_test.exs | 11 +++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex index dbbda53..0bf1be9 100644 --- a/lib/phoenix_html/form.ex +++ b/lib/phoenix_html/form.ex @@ -334,17 +334,18 @@ defmodule Phoenix.HTML.Form do [acc | option(option_key, option_value, [], selected_values)] options, acc when is_list(options) -> - {option_key, options} = Keyword.pop(options, :key) - - option_key || + if not Keyword.has_key?(options, :key) do raise ArgumentError, "expected :key key when building ) <> - ~s() + ~s() <> + ~s() assert options_for_select(["value", :hr, "novalue"], "novalue") |> safe_to_string() == ~s() <> @@ -261,14 +262,16 @@ defmodule Phoenix.HTML.FormTest do [ [value: "value", key: "Value", disabled: true], :hr, - [value: "novalue", key: "No Value"] + [value: "novalue", key: "No Value"], + [value: nil, key: nil] ], "novalue" ) |> safe_to_string() == ~s() <> ~s(
) <> - ~s() + ~s() <> + ~s() assert options_for_select(~w(value novalue), ["value", "novalue"]) |> safe_to_string() == ~s() <>