phoebe

phoebe

Text Input field with dynamic placeholder

Hi,

In my html.eex template form, I have a text_input form input and would like to pass to the placeholder a value based on some other field from the form (previously saved before form is rendered). I would like to do something like the following - have a :username field with a placeholder of the :first_name field value, but the placeholder variable is not recognised here:

<%= form_for @changeset, @action, fn f -> %>
  <%= text_input f, :username, placeholder: :first_name %>
  ... 
<% end %>

Is this possible to do with the text_input macro or do I need to use the tag macro? Thanks!

Marked As Solved

kokolegorille

kokolegorille

If You know the value of first_name, pass it as a variable in the controller…

:first_name is only an atom, pass it, and use it as @first_name.

If You want to use conditional rendering on the same form… You will need some JS skill… or LiveView, or Drab, or any server side libs that can potentially update DOM.

Also Liked

OvermindDL1

OvermindDL1

If you are wanting :first_name to pull from a key on your changeset/whatever named first_name then you can always do this:

<%= form_for @changeset, @action, fn f -> %>
  <%= text_input f, :username, placeholder: input_value(f, :first_name) %>
  ... 
<% end %>

And that will compile the value in as the placeholder. If you want it to update ‘on the page’ then that’s a Javascript thing, so either a some javascript, drab, live_view, etc… :slight_smile:

phoebe

phoebe

Thanks! Very helpful indeed, input_value is easier than passing variable into a controller

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is qui...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30840 112
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement