Created
November 22, 2022 22:13
-
-
Save kellyfelkins/ff3e27966e864f4f50cbd3bf60089960 to your computer and use it in GitHub Desktop.
`apply_action` if you want errors on your elixir form with embeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def handle_event("name-change", params, socket) do | |
# 1. fetch the previously assigned changeset | |
export_changeset = socket.assigns.export_changeset | |
# 2. apply the changes | |
export_changeset = Export.changeset(export_changeset, params["export"]) | |
# 3. the parent changeset is invalid, and there are errors in the child (embedded) changesets, | |
# but the parent changeset does not have errors | |
# 4. `apply_action` sets the action, and possibly other changes on the changeset | |
case Ecto.Changeset.apply_action(export_changeset, :insert) do | |
{:ok, data} -> | |
{:noreply, assign(socket, export_changeset: export_changeset)} | |
{:error, export_changeset} -> | |
# 5. now `inputs_for` has a top level errors value, | |
# and the error tag gets displayed | |
{:noreply, assign(socket, export_changeset: export_changeset)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment