View Source Doumi.Phoenix.Params
Doumi.Phoenix.Params
is a helper library that supports converting form to params and params to form.
도우미(Doumi) means "helper" in Korean.
usage
Usage
defmodule MyAppWeb.TestParams do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
embedded_schema do
field :foo, :string
end
@required [:foo]
def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, @required)
|> validate_required(@required)
end
end
defmodule MyAppWeb.MyLive do
use MyAppWeb, :live_view
alias MyAppWeb.TestParams
alias Doumi.Phoenix.Params
...
@impl true
def mount(_params, _session, socket) do
# Without Doumi.Phoenix.Params
# form =
# TestParams.changeset(%TestParams{}, %{})
# |> to_form(as: :test)
# With Doumi.Phoenix.Params
form = Params.to_form(%TestParams{}, %{}, as: :test, validate: false)
socket = socket |> assign(:form, form)
{:ok, socket}
end
@impl true
def handle_event("validate", %{"test" => test_params}, socket) do
# Without Doumi.Phoenix.Params
# form =
# TestParams.changeset(%TestParams{}, test_params)
# |> to_form(as: :test)
# |> Map.put(:action, :validate)
# With Doumi.Phoenix.Params
form = Params.to_form(%TestParams{}, test_params, as: :test)
socket = socket |> assign(:form, form)
{:noreply, socket}
end
@impl true
def handle_event("change_foo_from_outside_of_form", %{"foo" => foo}, socket) do
# Without Doumi.Phoenix.Params
# test_params =
# socket.assigns.form.params
# |> Map.put(%{"foo" => foo})
#
# form =
# TestParams.changeset(%TestParams{}, test_params)
# |> to_form(as: :test)
# |> Map.put(:action, :validate)
# With Doumi.Phoenix.Params
test_params =
socket.assigns.form
|> Params.to_params(%{"foo" => foo})
form = Params.to_form(%TestParams{}, test_params, as: :test)
socket = socket |> assign(:form, form)
{:noreply, socket}
end
@impl true
def handle_event("save", %{"test" => test_params}, socket) do
# Without Doumi.Phoenix.Params
# test_params_map =
# TestParams.changeset(%TestParams, test_params)
# |> Ecto.Changeset.apply_changes()
# |> some_how_change_nested_struct_to_map_if_you_use_map_with_atom_key()
# With Doumi.Phoenix.Params
test_params_map =
Params.to_form(%TestParams{}, test_params)
|> Params.to_map()
:ok = TestDomain.create_test(test_params_map)
{:noreplym, socket}
end
...
end
Here's the working example
- page: https://json.media/playgrounds/form
- code: https://github.com/nallwhy/json_corp/blob/main/apps/json_corp_web/lib/json_corp_web/live/playgrounds/form_live.ex
installation
Installation
The package can be installed by adding doumi_phoenix_params
to your list of dependencies in mix.exs
:
def deps do
[
{:doumi_phoenix_params, "~> 0.2.0"}
]
end
other-doumi-packages
Other Doumi packages
- Doumi.Phoenix.SVG: A helper library that generates Phoenix function components from SVG files.
- Doumi.URI.Query: A helper library that encode complicated query of URI.
copyright-and-license
Copyright and License
Copyright (c) 2023 Jinkyou Son (Json)
This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.