Skip to content

Commit

Permalink
Fix docs and deprecate entries in inputs_for
Browse files Browse the repository at this point in the history
Closes #425.
  • Loading branch information
josevalim committed Aug 10, 2023
1 parent ed9a2a1 commit 9464dfb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 72 deletions.
8 changes: 4 additions & 4 deletions lib/phoenix_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 9 additions & 30 deletions lib/phoenix_html/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ defmodule Phoenix.HTML.Form do
bin
end

bin |> String.replace("_", " ") |> String.capitalize()
bin |> String.replace("_", " ") |> :string.titlecase()
end

@doc false
Expand Down Expand Up @@ -596,39 +596,18 @@ defmodule Phoenix.HTML.Form do
html_escape([form_tag(action, form.options), fun.(form), raw("</form>")])
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 %>
</.form>
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])
Expand All @@ -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
Expand Down
38 changes: 0 additions & 38 deletions test/phoenix_html/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(<input id="user_company_name" name="user[company][name]" type="text">)
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(<input id="custom_id_name" name="new_company[name]" type="text">)
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 =
Expand Down

0 comments on commit 9464dfb

Please sign in to comment.