Repository: phoenixframework/phoenix_html (elixir)

Issue:
Rendering array field as hidden inputs
I'm missing a way to automatically render an array field as hidden inputs. What I'd like to be able to do is this:

```elixir
<%= form_for :some_form, "/", [hidden: [some_field: ["one", "two", "three"]]], fn f -> %>
  <%= hidden_inputs_for f %>
<% end %>
```

Which should render to:

```html
<form action="/" method="post">
  <input type="hidden" id="something_0" name="something[]" value="one" />
  <input type="hidden" id="something_1" name="something[]" value="two" />
  <input type="hidden" id="something_2" name="something[]" value="three" />
</form>
```

Would it make sense to modify `hidden_inputs_for/1` to behave that way? If so, I can open a PR.

Background: I'm currently working on tighter integration between [flop](https://github.com/woylie/flop), [flop_phoenix](https://github.com/woylie/flop_phoenix) and `Phoenix.HTML.Form` by implementing `Phoenix.HTML.FormData` for a struct. The goal is to make setting up filter forms based on the library structs easier. To work well with the sortable table and pagination components, parameters like the page size etc. are added to the `:hidden` field in `form_for/2`, so that all of those parameters can be rendered with a single call to `hidden_inputs_for/1`. For sorting however, an `:order_by` field is used, which is a list. I tried modifying the protocol implementation to include that list as hidden fields as well, but I wasn't successful, which means at the moment I would have to add a modified `hidden_inputs_for/1` function to the my library. I'd rather have this part of Phoenix.HTML.

Pull request:
support rendering hidden inputs for list of values
Something like this?

resolves #359 

Patch:
diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex
index 9f0860e..d49facd 100644
--- a/lib/phoenix_html/form.ex
+++ b/lib/phoenix_html/form.ex
@@ -631,11 +631,28 @@ defmodule Phoenix.HTML.Form do
   """
   @spec hidden_inputs_for(t) :: list(Phoenix.HTML.safe())
   def hidden_inputs_for(form) do
-    for {k, v} <- form.hidden do
-      hidden_input(form, k, value: v)
+    Enum.flat_map(form.hidden, fn {k, v} ->
+      hidden_inputs_for(form, k, v)
+    end)
+  end
+
+  defp hidden_inputs_for(form, k, values) when is_list(values) do
+    id = input_id(form, k)
+    name = input_name(form, k)
+
+    for {v, index} <- Enum.with_index(values) do
+      hidden_input(form, k,
+        id: id <> "_" <> Integer.to_string(index),
+        name: name <> "[]",
+        value: v
+      )
     end
   end
 
+  defp hidden_inputs_for(form, k, v) do
+    [hidden_input(form, k, value: v)]
+  end
+
   @doc """
   Generates an email input.
 


Test patch:
diff --git a/test/phoenix_html/form_test.exs b/test/phoenix_html/form_test.exs
index f637936..0b3dde0 100644
--- a/test/phoenix_html/form_test.exs
+++ b/test/phoenix_html/form_test.exs
@@ -509,6 +509,17 @@ defmodule Phoenix.HTML.FormTest do
 
       assert hidden_inputs_for(form) == [hidden_input(form, :id, value: 1)]
     end
+
+    test "generates hidden fields for lists from the given form" do
+      form = %{form_for(conn(), "/") | hidden: [field: ["a", "b", "c"]]}
+
+      assert hidden_inputs_for(form) ==
+               [
+                 hidden_input(form, :field, name: "field[]", id: "field_0", value: "a"),
+                 hidden_input(form, :field, name: "field[]", id: "field_1", value: "b"),
+                 hidden_input(form, :field, name: "field[]", id: "field_2", value: "c")
+               ]
+    end
   end
 
   ## email_input/3