From a41958f386ca629fcd5ffcadfe259f88b78cb639 Mon Sep 17 00:00:00 2001 From: Derek Schneider Date: Wed, 21 Feb 2018 22:35:12 -0500 Subject: [PATCH] input_name should return a String when passed an unnamed form. - This bug shows up when using multiple_select with an unnamed form. - Function spec indicates that input_name returns a String. --- lib/phoenix_html/form.ex | 2 +- test/phoenix_html/form_test.exs | 39 ++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex index b8ea17fd..cdcef522 100644 --- a/lib/phoenix_html/form.ex +++ b/lib/phoenix_html/form.ex @@ -361,7 +361,7 @@ defmodule Phoenix.HTML.Form do """ @spec input_name(t | atom, field) :: String.t def input_name(%{name: nil}, field), - do: field + do: to_string(field) def input_name(%{name: name}, field) when is_atom(field) or is_binary(field), do: "#{name}[#{field}]" def input_name(name, field) when is_atom(name) and is_atom(field) or is_binary(field), diff --git a/test/phoenix_html/form_test.exs b/test/phoenix_html/form_test.exs index b90ec117..908b6b67 100644 --- a/test/phoenix_html/form_test.exs +++ b/test/phoenix_html/form_test.exs @@ -9,11 +9,11 @@ defmodule Phoenix.HTML.FormTest do A function that executes `form_for/4` and extracts its inner contents for assertion. """ - def safe_form(fun, opts \\ []) do + def safe_form(fun, opts \\ [as: :search]) do mark = "--PLACEHOLDER--" contents = - safe_to_string form_for(conn(), "/", [as: :search] ++ opts, fn f -> + safe_to_string form_for(conn(), "/", opts, fn f -> html_escape [mark, fun.(f), mark] end) @@ -238,7 +238,7 @@ defmodule Phoenix.HTML.FormTest do safe_form(&file_input(&1, :key)) end - assert safe_form(&file_input(&1, :key), multipart: true) == + assert safe_form(&file_input(&1, :key), multipart: true, as: :search) == ~s() end @@ -700,6 +700,39 @@ defmodule Phoenix.HTML.FormTest do ~s() end + test "multiple_select/4 with unnamed form" do + assert safe_form(&multiple_select(&1, :key, [{"foo", 1}, {"bar", 2}], value: [1], selected: [2]), []) == + ~s() + + assert safe_form(&multiple_select(&1, :other, [{"foo", 1}, {"bar", 2}], selected: [2]), []) == + ~s() + + assert safe_form(&multiple_select(&1, :key, [{"foo", 1}, {"bar", 2}], value: [2]), []) == + ~s() + + assert safe_form(&multiple_select(&1, :key, ~w(value novalue), value: ["novalue"]), []) == + ~s() + + assert safe_form(&multiple_select(put_in(&1.params["key"], ["3"]), :key, [{"foo", 1}, {"bar", 2}, {"goo", 3}], selected: [2]), []) == + ~s() + end + # date_select/4 test "date_select/4" do