Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document form[field] access quirks #419

Merged
merged 2 commits into from
Mar 16, 2023

Conversation

rktjmp
Copy link
Contributor

@rktjmp rktjmp commented Mar 16, 2023

field. Forms backed by an Ecto.Changeset only support atom field names.

The form handles the string fine, but the ecto formdata implementation raises on non atom keys.

Per:

 defp fetch(%{errors: errors} = form, field, field_as_string) do
    {:ok,
     %Phoenix.HTML.FormField{
       errors: for({^field, value} <- errors, do: value),
       field: field,
       form: form,
       id: input_id(form, field_as_string),
       name: input_name(form, field_as_string),
       value: input_value(form, field) # <- THIS CALL
     }}
  end

and

    def input_value(_data, _form, field) do
      raise ArgumentError, "expected field to be an atom, got: #{inspect(field)}"
    end

https://github.com/phoenixframework/phoenix_ecto/blob/c68548d8d2dd904cbb7e43cf10b11076b92df91d/lib/phoenix_ecto/html.ex#L121-L123

It is possible to "access" fields which do not exist in the source data structure. A Phoenix.HTML.FormField struct will be dynamically created with some attributes such as name and id populated.

A useful quirk/feature that may also catch some people out.

rktjmp added 2 commits March 16, 2023 19:00

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@rktjmp rktjmp changed the title Doc form access Document form[field] access quirks Mar 16, 2023
@josevalim josevalim merged commit 4810b0a into phoenixframework:main Mar 16, 2023
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants