nallwhy
Doumi.Phoenix.Params - A helper library that supports converting form to params and params to form
Doumi.Phoenix.Params is a helper library that supports converting form to params and params to form.
It is still confusing to handle Ecto.Changeset, Phoenix.HTML.Form for form binding.
For example,
To make it easy and straightforward, I’ve made Doumi.Phoenix.Params.
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.source.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
Most Liked
nallwhy
v0.3.0 is released!
Now it provides use Doumi.Phoenix.Params, opts... that creates handy to_form/2 to the schema module.
defmodule MyAppWeb.TestParams do
use Ecto.Schema
use Doumi.Phoenix.Params, as: :test # <--- with use macro
...
end
defmodule MyAppWeb.MyLive do
...
@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)
# With use Doumi.Phoenix.Params
form = TestParams.to_form(test_params) <--- much easier
socket = socket |> assign(:form, form)
{:noreply, socket}
end
end
Thank you for reading it!
1
Popular in Announcing
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM.
...
New
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New
I’m excited to announce Jido, a framework providing foundational primitives for building autonomous agent systems in Elixir. While develo...
New
Hello everyone,
I wrote a small library today called MapDiff.
It returns a map listing the (smallest amount of) changes to get from map...
New
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections.
Fo...
New
Hey everyone!
Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
Hello all,
I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects.
Core ideas
Type- and function specifications are const...
New
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
New
Prometheus metrics and Grafana dashboards for all of your favorite Elixir libraries
I have been put...
New
Other popular topics
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Got a question about when to concat vs. prepending items to list then reversing to achieve appending.
So i know lists boil down to [1 | ...
New
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New








