Skip to content

Commit

Permalink
Document and pass through option accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 17, 2024
1 parent a8262ad commit b6d63e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions lib/phoenix_html/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ defmodule Phoenix.HTML.Form do
* `:source` - the data structure that implements the form data protocol
* `:action` - The action that was taken against the form. This value can be
used to distinguish between different operations such as the user typing
into a form for validation, or submitting a form for a database insert.
* `:impl` - the module with the form data protocol implementation.
This is used to avoid multiple protocol dispatches.
Expand Down
18 changes: 9 additions & 9 deletions lib/phoenix_html/form_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ defprotocol Phoenix.HTML.FormData do
end

defimpl Phoenix.HTML.FormData, for: Map do
def to_form(conn_or_atom_or_map, opts) do
{name, params, opts} = name_params_and_opts(conn_or_atom_or_map, opts)
def to_form(map, opts) do
{name, params, opts} = name_params_and_opts(map, opts)
{errors, opts} = Keyword.pop(opts, :errors, [])
{action, opts} = Keyword.pop(opts, :action, nil)
id = Keyword.get(opts, :id) || name
Expand All @@ -89,7 +89,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
end

%Phoenix.HTML.Form{
source: conn_or_atom_or_map,
source: map,
impl: __MODULE__,
id: id,
name: name,
Expand All @@ -115,14 +115,14 @@ defimpl Phoenix.HTML.FormData, for: Map do
end
end

def to_form(conn_or_atom_or_map, form, field, opts) when is_atom(field) or is_binary(field) do
def to_form(map, form, field, opts) when is_atom(field) or is_binary(field) do
{default, opts} = Keyword.pop(opts, :default, %{})
{prepend, opts} = Keyword.pop(opts, :prepend, [])
{append, opts} = Keyword.pop(opts, :append, [])
{name, opts} = Keyword.pop(opts, :as)
{id, opts} = Keyword.pop(opts, :id)
{hidden, opts} = Keyword.pop(opts, :hidden, [])
{action, opts} = Keyword.pop(opts, :action)
{action, opts} = Keyword.pop(opts, :action, form.action)

id = to_string(id || form.id <> "_#{field}")
name = to_string(name || form.name <> "[#{field}]")
Expand All @@ -133,7 +133,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
is_map(default) ->
[
%Phoenix.HTML.Form{
source: conn_or_atom_or_map,
source: map,
impl: __MODULE__,
id: id,
name: name,
Expand All @@ -160,7 +160,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
index_string = Integer.to_string(index)

%Phoenix.HTML.Form{
source: conn_or_atom_or_map,
source: map,
impl: __MODULE__,
index: index,
action: action,
Expand All @@ -175,7 +175,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
end
end

def input_value(_conn_or_atom_or_map, %{data: data, params: params}, field)
def input_value(_map, %{data: data, params: params}, field)
when is_atom(field) or is_binary(field) do
key = field_to_string(field)

Expand All @@ -185,7 +185,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
end
end

def input_validations(_conn_or_atom_or_map, _form, _field), do: []
def input_validations(_map, _form, _field), do: []

# Normalize field name to string version
defp field_to_string(field) when is_atom(field), do: Atom.to_string(field)
Expand Down
11 changes: 8 additions & 3 deletions test/phoenix_html/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ defmodule Phoenix.HTML.FormTest do
}
}

form = Phoenix.HTML.FormData.to_form(map, as: "search")
form = Phoenix.HTML.FormData.to_form(map, as: "search", action: :validate)
Phoenix.HTML.FormData.to_form(map, form, field, opts)
end

Expand All @@ -329,6 +329,7 @@ defmodule Phoenix.HTML.FormTest do
id: "search_unknown_year",
name: "search[unknown][year]",
field: :year,
form: %{action: :validate},
value: nil
} = f[:year]
end
Expand All @@ -340,6 +341,7 @@ defmodule Phoenix.HTML.FormTest do
id: "search_date_year",
name: "search[date][year]",
field: :year,
form: %{action: :validate},
value: "2020"
} = f[:year]
end
Expand All @@ -351,6 +353,7 @@ defmodule Phoenix.HTML.FormTest do
id: "search_unknown_year",
name: "search[unknown][year]",
field: :year,
form: %{action: :validate},
value: 2015
} = f[:year]
end
Expand All @@ -362,17 +365,19 @@ defmodule Phoenix.HTML.FormTest do
id: "search_date_year",
name: "search[date][year]",
field: :year,
form: %{action: :validate},
value: "2020"
} = f[:year]
end

test "one: with custom name and id" do
[f] = nested_form(:date, as: :foo, id: :bar)
test "one: with custom name, id, and action" do
[f] = nested_form(:date, as: :foo, id: :bar, action: :another)

assert %Phoenix.HTML.FormField{
id: "bar_year",
name: "foo[year]",
field: :year,
form: %{action: :another},
value: "2020"
} = f[:year]
end
Expand Down

0 comments on commit b6d63e7

Please sign in to comment.