i-n-g-m-a-r

i-n-g-m-a-r

Phoenix.HTML.Form pass custom assigns (key={value})

Suppose one defines a simple form like this:

<.simple_form :let={f} for={@changeset} focus={@focus}>

Then how would one collect the @focus value when defining an input:

<.input field={f[:name]} type="text" focus={@focus} />

Annotating simple_form/1 does not work.

attr :focus, :atom, default: nil
<.form :let={f} for={@for} as={@as} focus={@focus} {@rest}>

Phoenix.HTML.Form does not pass @focus or any custom value along.

Using @rest results in @focus being output as a form attribute.

So how does one trickle down key={value} ?

Many thanks.

First Post!

LostKobrakai

LostKobrakai

You don’t need to pass the value to <.simple_form> in the first place. You can pass it to the input directly and that’s enough.

Last Post!

i-n-g-m-a-r

i-n-g-m-a-r

Thanks again.

So in conclusion as long as we are in control of a function we can define key={value} syntax.
When using form/1 or other functions out of our control we cannot use it.

Unless we do weird stuff like:

<.simple_form :let={f} for={@changeset} data-focus={get_focus(@changeset, [:field_b, :field_c, :field_a])}>

and

<input type={@type} autofocus={has_focus?(@__given__)}>

and

  def has_focus?(%{__given__: %{field: %{field: name, form: %{options: opts}}}}) do
    opts[:"data-focus"] == name
  end

  def has_focus?(_), do: false

and accept a data-focus attribute on our form.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lastday4you
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
bsollish-terakeet
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Darmani72
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement