Skip to content

Commit

Permalink
Improve to_form docs
Browse files Browse the repository at this point in the history
Closes #2469.
  • Loading branch information
josevalim committed Feb 18, 2023
1 parent ee93778 commit 52a49f1
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/phoenix_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1381,16 +1381,20 @@ defmodule Phoenix.Component do
{:noreply, assign(socket, form: to_form(params))}
end
However, most typically, we specify a name to nest the parameters:
When you pass a map to `to_form/1`, it assumes said map contains
the form parameters, which are expected to have string keys.
You can also specify a name to nest the parameters:
def handle_event("submitted", %{"user" => user_params}, socket) do
{:noreply, assign(socket, form: to_form(user_params, as: :user))}
end
## Creating a form from changesets
When using changesets, the name `:as` is automatically retrieved
from the schema. For example, if you have a user schema:
When using changesets, the underlying data, form parameters, and
errors are retrieved from it. The `:as` option is automatically
computed too. For example, if you have a user schema:
defmodule MyApp.Users.User do
use Ecto.Schema
Expand All @@ -1406,10 +1410,8 @@ defmodule Phoenix.Component do
|> Ecto.Changeset.change()
|> to_form()
Phoenix will take care of getting all of the relevant information
from the changeset, including errors, data, and names for you.
In this case, the parameters will be available under
`%{"post" => post_params}`.
In this case, once the form is submitted, the parameters will
be available under `%{"post" => post_params}`.
## Options
Expand All @@ -1426,7 +1428,7 @@ defmodule Phoenix.Component do
Then the remaining options are merged with the existing
form options.
"""
def to_form(data, options \\ [])
def to_form(data_or_params, options \\ [])

def to_form(%Phoenix.HTML.Form{} = data, options) do
{name, id} =
Expand Down

0 comments on commit 52a49f1

Please sign in to comment.