Skip to content

Commit b6d63e7

Browse files
committed
Document and pass through option accordingly
1 parent a8262ad commit b6d63e7

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

lib/phoenix_html/form.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ defmodule Phoenix.HTML.Form do
3636
3737
* `:source` - the data structure that implements the form data protocol
3838
39+
* `:action` - The action that was taken against the form. This value can be
40+
used to distinguish between different operations such as the user typing
41+
into a form for validation, or submitting a form for a database insert.
42+
3943
* `:impl` - the module with the form data protocol implementation.
4044
This is used to avoid multiple protocol dispatches.
4145

lib/phoenix_html/form_data.ex

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ defprotocol Phoenix.HTML.FormData do
7878
end
7979

8080
defimpl Phoenix.HTML.FormData, for: Map do
81-
def to_form(conn_or_atom_or_map, opts) do
82-
{name, params, opts} = name_params_and_opts(conn_or_atom_or_map, opts)
81+
def to_form(map, opts) do
82+
{name, params, opts} = name_params_and_opts(map, opts)
8383
{errors, opts} = Keyword.pop(opts, :errors, [])
8484
{action, opts} = Keyword.pop(opts, :action, nil)
8585
id = Keyword.get(opts, :id) || name
@@ -89,7 +89,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
8989
end
9090

9191
%Phoenix.HTML.Form{
92-
source: conn_or_atom_or_map,
92+
source: map,
9393
impl: __MODULE__,
9494
id: id,
9595
name: name,
@@ -115,14 +115,14 @@ defimpl Phoenix.HTML.FormData, for: Map do
115115
end
116116
end
117117

118-
def to_form(conn_or_atom_or_map, form, field, opts) when is_atom(field) or is_binary(field) do
118+
def to_form(map, form, field, opts) when is_atom(field) or is_binary(field) do
119119
{default, opts} = Keyword.pop(opts, :default, %{})
120120
{prepend, opts} = Keyword.pop(opts, :prepend, [])
121121
{append, opts} = Keyword.pop(opts, :append, [])
122122
{name, opts} = Keyword.pop(opts, :as)
123123
{id, opts} = Keyword.pop(opts, :id)
124124
{hidden, opts} = Keyword.pop(opts, :hidden, [])
125-
{action, opts} = Keyword.pop(opts, :action)
125+
{action, opts} = Keyword.pop(opts, :action, form.action)
126126

127127
id = to_string(id || form.id <> "_#{field}")
128128
name = to_string(name || form.name <> "[#{field}]")
@@ -133,7 +133,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
133133
is_map(default) ->
134134
[
135135
%Phoenix.HTML.Form{
136-
source: conn_or_atom_or_map,
136+
source: map,
137137
impl: __MODULE__,
138138
id: id,
139139
name: name,
@@ -160,7 +160,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
160160
index_string = Integer.to_string(index)
161161

162162
%Phoenix.HTML.Form{
163-
source: conn_or_atom_or_map,
163+
source: map,
164164
impl: __MODULE__,
165165
index: index,
166166
action: action,
@@ -175,7 +175,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
175175
end
176176
end
177177

178-
def input_value(_conn_or_atom_or_map, %{data: data, params: params}, field)
178+
def input_value(_map, %{data: data, params: params}, field)
179179
when is_atom(field) or is_binary(field) do
180180
key = field_to_string(field)
181181

@@ -185,7 +185,7 @@ defimpl Phoenix.HTML.FormData, for: Map do
185185
end
186186
end
187187

188-
def input_validations(_conn_or_atom_or_map, _form, _field), do: []
188+
def input_validations(_map, _form, _field), do: []
189189

190190
# Normalize field name to string version
191191
defp field_to_string(field) when is_atom(field), do: Atom.to_string(field)

test/phoenix_html/form_test.exs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ defmodule Phoenix.HTML.FormTest do
314314
}
315315
}
316316

317-
form = Phoenix.HTML.FormData.to_form(map, as: "search")
317+
form = Phoenix.HTML.FormData.to_form(map, as: "search", action: :validate)
318318
Phoenix.HTML.FormData.to_form(map, form, field, opts)
319319
end
320320

@@ -329,6 +329,7 @@ defmodule Phoenix.HTML.FormTest do
329329
id: "search_unknown_year",
330330
name: "search[unknown][year]",
331331
field: :year,
332+
form: %{action: :validate},
332333
value: nil
333334
} = f[:year]
334335
end
@@ -340,6 +341,7 @@ defmodule Phoenix.HTML.FormTest do
340341
id: "search_date_year",
341342
name: "search[date][year]",
342343
field: :year,
344+
form: %{action: :validate},
343345
value: "2020"
344346
} = f[:year]
345347
end
@@ -351,6 +353,7 @@ defmodule Phoenix.HTML.FormTest do
351353
id: "search_unknown_year",
352354
name: "search[unknown][year]",
353355
field: :year,
356+
form: %{action: :validate},
354357
value: 2015
355358
} = f[:year]
356359
end
@@ -362,17 +365,19 @@ defmodule Phoenix.HTML.FormTest do
362365
id: "search_date_year",
363366
name: "search[date][year]",
364367
field: :year,
368+
form: %{action: :validate},
365369
value: "2020"
366370
} = f[:year]
367371
end
368372

369-
test "one: with custom name and id" do
370-
[f] = nested_form(:date, as: :foo, id: :bar)
373+
test "one: with custom name, id, and action" do
374+
[f] = nested_form(:date, as: :foo, id: :bar, action: :another)
371375

372376
assert %Phoenix.HTML.FormField{
373377
id: "bar_year",
374378
name: "foo[year]",
375379
field: :year,
380+
form: %{action: :another},
376381
value: "2020"
377382
} = f[:year]
378383
end

0 commit comments

Comments
 (0)