Input Helper prepopulated value

I read a blog post from Jose on plataformatec[1] about dynamic forms to add custom input helper functions. This works great so far.

But I am missing the possibility to set value: to pre-populate the input with a given string like in the docs:

<%= text_input :user, :name, value: "This is a prepopulated value" %>

Anybody here able to help?

[1] Dynamic forms with Phoenix « Plataformatec Blog

That example is using an atom as form - which is supported, but isn’t going to do much with the dynamic input generation since that implementation of FormData just returns :text_input:

OTOH using value: with a form backed by a Changeset is not usually what you want. :thinking:

In any case, you’ll want to look into extracting opts[:value] (if it’s passed to input) and then including it in input_opts.

1 Like

Actually, what you describe here is what I am doing:

input_opts = [
  class: "border rounded #{state_class(form, field)} #{opts[:class_input]}"
]

if opts[:value] do
  input_opts = Keyword.merge(input_opts, value: opts[:value])
end

but the field remains empty (as the record does not exist yet). I suppose it gets overridden by the actual input = apply(Phoenix.HTML.Form, type, [form, field, input_opts]) call, but I am not sure.

To your question about why: I do have another table with customer input and similar fields. I want to give some kind of “suggestion” here from the user input, or more specific the input for the new record shall somehow be pre-populated with the data as a suggestion.