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
Hello :waving_hand:
Allow me to introduce you to Tz, an alternative time zone database support to Tzdata.
Why another library?
First a...
New
Hello everyone!
I am excited to share our heart project Backpex with you.
After building several Phoenix applications, we realized that...
New
Hi there,
for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
ExApi is a library that I’m developing now and hope release soon
This library will allow to:
list all apis
list all api implementation...
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
Dune is a sandbox for Elixir and aims to safely evaluate user-provided code.
You can try it out using this basic Elixir playground made ...
New
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New
Hello everybody.
I have just released Jason - a new JSON library.
You might be wondering, why do we need a new library? The primary foc...
New
Hey guyz
We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it.
Have...
New
Other popular topics
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
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
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









