From 9464dfb7a493cd91803e6aefdbe94fb69194c668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 10 Aug 2023 18:55:18 +0200 Subject: [PATCH] Fix docs and deprecate entries in inputs_for Closes #425. --- lib/phoenix_html.ex | 8 +++---- lib/phoenix_html/form.ex | 39 ++++++++------------------------- test/phoenix_html/form_test.exs | 38 -------------------------------- 3 files changed, 13 insertions(+), 72 deletions(-) diff --git a/lib/phoenix_html.ex b/lib/phoenix_html.ex index df50d9c..74d2374 100644 --- a/lib/phoenix_html.ex +++ b/lib/phoenix_html.ex @@ -207,11 +207,11 @@ defmodule Phoenix.HTML do iex> safe_to_string attributes_escape(title: "the title", id: "the id", selected: true) " title=\"the title\" id=\"the id\" selected" - iex> safe_to_string attributes_escape(%{data: [confirm: "Are you sure?"], class: "foo"}) - " class=\"foo\" data-confirm=\"Are you sure?\"" + iex> safe_to_string attributes_escape(%{data: [confirm: "Are you sure?"]}) + " data-confirm=\"Are you sure?\"" - iex> safe_to_string attributes_escape(%{phx: [value: [foo: "bar"]], class: "foo"}) - " class=\"foo\" phx-value-foo=\"bar\"" + iex> safe_to_string attributes_escape(%{phx: [value: [foo: "bar"]]}) + " phx-value-foo=\"bar\"" """ def attributes_escape(attrs) when is_list(attrs) do diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex index ca176a8..ddf2177 100644 --- a/lib/phoenix_html/form.ex +++ b/lib/phoenix_html/form.ex @@ -373,7 +373,7 @@ defmodule Phoenix.HTML.Form do bin end - bin |> String.replace("_", " ") |> String.capitalize() + bin |> String.replace("_", " ") |> :string.titlecase() end @doc false @@ -596,39 +596,18 @@ defmodule Phoenix.HTML.Form do html_escape([form_tag(action, form.options), fun.(form), raw("")]) end - @doc """ - Same as `inputs_for(form, field, [])`. - """ - @spec inputs_for(t, field) :: list(Phoenix.HTML.Form.t()) + @doc false def inputs_for(form, field) when is_atom(field) or is_binary(field), do: inputs_for(form, field, []) - @doc """ - Generate a new form builder for the given parameter in a form **without** an - anonymous function. - - This functionality exists mostly for integration with `Phoenix.LiveView` - that replaces the anonymous function for returning the generated forms - instead. - - Keep in mind that this function does not generate hidden inputs automatically - like `inputs_for/4`. To generate them, you need to explicitly do it yourself. - - <.form for={@changeset} action={Routes.user_path(@conn, :create)}> - Name: <%= text_input f, :name %> - - <%= for friend_form <- inputs_for(f, :friends) do %> - <%!-- Generating hidden inputs --!%> - <%= hidden_inputs_for(friend_form) %> - <%= text_input friend_form, :name %> - <% end %> - - - See `inputs_for/4` for the available options. - """ - @spec inputs_for(t, field, Keyword.t()) :: list(Phoenix.HTML.Form.t()) + @doc false def inputs_for(%{impl: impl} = form, field, options) when (is_atom(field) or is_binary(field)) and is_list(options) do + IO.warn( + "inputs_for/3 without an anonymous function is deprecated. " <> + "If you are using Phoenix.LiveView, use the new Phoenix.Component.inputs_for/1 component" + ) + options = form.options |> Keyword.take([:multipart]) @@ -640,7 +619,7 @@ defmodule Phoenix.HTML.Form do @doc """ Generate a new form builder for the given parameter in form. - See the module documentation for examples of using this function. + See `form_for/4` for examples of using this function. ## Options diff --git a/test/phoenix_html/form_test.exs b/test/phoenix_html/form_test.exs index fc08f22..4259ac5 100644 --- a/test/phoenix_html/form_test.exs +++ b/test/phoenix_html/form_test.exs @@ -536,44 +536,6 @@ defmodule Phoenix.HTML.FormTest do end end - describe "inputs_for/3" do - test "generate a new form builder for the given parameter" do - form = - form_for(%{}, "/", [as: :user], fn form -> - for company_form <- inputs_for(form, :company) do - text_input(company_form, :name) - end - end) - |> safe_to_string() - - assert form =~ ~s() - end - - test "support options" do - form = - form_for(%{}, "/", [as: :user], fn form -> - for company_form <- inputs_for(form, :company, as: :new_company, id: :custom_id) do - text_input(company_form, :name) - end - end) - |> safe_to_string() - - assert form =~ ~s() - end - - test "support atom or binary field" do - form = Phoenix.HTML.FormData.to_form(%{}, as: :user) - - [f] = inputs_for(form, :key) - assert f.name == "user[key]" - assert f.id == "user_key" - - [f] = inputs_for(form, "key") - assert f.name == "user[key]" - assert f.id == "user_key" - end - end - describe "inputs_for/4" do test "generate a new form builder for the given parameter" do form =