Skip to content

Commit f829bcb

Browse files
committed
Fix input_changed?
1 parent da9c164 commit f829bcb

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

lib/phoenix_html/form.ex

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ defmodule Phoenix.HTML.Form do
6464
automatically show up as selected in the form.
6565
6666
### A note on `:errors`
67+
6768
Even if `changeset.errors` is non-empty, errors will not be displayed in a
6869
form if [the changeset
6970
`:action`](https://hexdocs.pm/ecto/Ecto.Changeset.html#module-changeset-actions)
@@ -278,13 +279,13 @@ defmodule Phoenix.HTML.Form do
278279
field_as_string = Atom.to_string(field)
279280

280281
{:ok,
281-
%Phoenix.HTML.FormField{
282-
errors: Keyword.get_values(errors, field),
283-
form: form,
284-
id: input_id(form, field_as_string),
285-
name: input_name(form, field_as_string),
286-
value: input_value(form, field_as_string)
287-
}}
282+
%Phoenix.HTML.FormField{
283+
errors: Keyword.get_values(errors, field),
284+
form: form,
285+
id: input_id(form, field_as_string),
286+
name: input_name(form, field_as_string),
287+
value: input_value(form, field_as_string)
288+
}}
288289
end
289290

290291
def get(%Form{}, field) do
@@ -378,15 +379,16 @@ defmodule Phoenix.HTML.Form do
378379
"""
379380
@spec input_changed?(t, t, atom) :: boolean()
380381
def input_changed?(
381-
%Form{impl: impl1, errors: errors1, source: source1} = form1,
382-
%Form{impl: impl2, errors: errors2, source: source2} = form2,
382+
%Form{impl: impl1, id: id1, name: name1, errors: errors1, source: source1} = form1,
383+
%Form{impl: impl2, id: id2, name: name2, errors: errors2, source: source2} = form2,
383384
field
384385
)
385386
when is_atom(field) do
386387
as_string = Atom.to_string(field)
387388

388-
impl1 == impl2 and Keyword.get_values(errors1, field) == Keyword.get_values(errors2, field) and
389-
impl1.input_value(source1, form1, as_string) == impl2.input_value(source2, form2, as_string)
389+
impl1 != impl2 or id1 != id2 or name1 != name2 or
390+
Keyword.get_values(errors1, field) != Keyword.get_values(errors2, field) or
391+
impl1.input_value(source1, form1, as_string) != impl2.input_value(source2, form2, as_string)
390392
end
391393

392394
@doc """

test/phoenix_html/form_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,19 @@ defmodule Phoenix.HTML.FormTest do
209209
assert safe_to_string(normalize_value("textarea", 1234)) == "\n1234"
210210
assert safe_to_string(normalize_value("textarea", nil)) == "\n"
211211
end
212+
213+
test "for anything else" do
214+
assert normalize_value("foo", "<other>") == "<other>"
215+
end
216+
end
217+
218+
test "input_changed?" do
219+
form = form(%{})
220+
refute input_changed?(form, form, :foo)
221+
assert input_changed?(form, %{form | errors: [foo: "bar"]}, :foo)
222+
assert input_changed?(form, %{form | name: "another"}, :foo)
223+
assert input_changed?(form, %{form | id: "another"}, :foo)
224+
assert input_changed?(form, form(%{"foo" => "bar"}), :foo)
212225
end
213226

214227
describe "access" do

0 commit comments

Comments
 (0)